wjarjoui
wjarjoui•14mo ago

Doc validator

How would I specify a doc type for the argument validator of a convex action? E.g. something along the lines of
args: {
item: v.object(Doc("items"))
}
args: {
item: v.object(Doc("items"))
}
12 Replies
Michal Srb
Michal Srb•14mo ago
Hey @wjarjoui, we don't have this. Usually you either want to send in the value required to create or update a document, or you'd send in an ID or list of IDs, and load the documents inside the function. This ensures that they are the latest versions of the documents. What's your use case? This article might be helpful: https://stack.convex.dev/types-cookbook it shows how to reuse validators between schema and args
Types and Validators in TypeScript: A Convex Cookbook
It can be tough to wrangle types to behave how you want them to. Thankfully, Convex was designed to make the experience with types perfect. Learn why ...
wjarjoui
wjarjouiOP•14mo ago
I have a calling action that already does this query to get the document, so I am trying to avoid doing it again in the called action. Although I see your point, and from prior understanding those queries end up being very cheap so perhaps best to just query again.
Michal Srb
Michal Srb•14mo ago
Yup, presumably you're scheduling the called action, so the documents could change between the scheduling and when the called action runs.
wjarjoui
wjarjouiOP•14mo ago
correct. I hadn't thought about that because they almost never change, but I like this safetyguard. On another note, would be great if there were easier ways to specify schemas for the v.object type, e.g. if I can pass a type instead of a schema
Michal Srb
Michal Srb•14mo ago
Agreed, being able to work with TypeScript types and have them compile to JS validators would be really nice (both for argument and schema validation).
wjarjoui
wjarjouiOP•14mo ago
On another note, I am trying to load a json object from a file in my convex module:
import * as OffchainFundABI from '../src/web3/ABI/IOffChainFund.json'
import * as OffchainFundABI from '../src/web3/ABI/IOffChainFund.json'
and I get the error:
Cannot find module '../src/web3/ABI/IOffChainFund.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
Cannot find module '../src/web3/ABI/IOffChainFund.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
The flag resolveJsonModule is already in my project's ts.config, do I need to add it to convex.json too? Trying to find a page for all the convex.json options, if that's available please point me to it
Michal Srb
Michal Srb•14mo ago
You might need a Node action for this, I'm not sure our esbuild setup allows loading json files.
wjarjoui
wjarjouiOP•14mo ago
It's actually running OK just the typescript is complaining. Either way, is there an outline of the full config options for convex.json?
ian
ian•14mo ago
I've already made a helper for making these objects 🎉 . Article just went out yesterday: https://stack.convex.dev/argument-validation-without-repetition
Argument Validation without Repetition
In the first post in this series, the Types and Validators cookbook, we shared several basic patterns & bes...
Michal Srb
Michal Srb•14mo ago
There are only two options: https://docs.convex.dev/production/project-configuration#changing-the-convex-folder-name-or-location and https://docs.convex.dev/functions/bundling#specifying-external-packages We should have a doc page that lists them together though, that's good feedback.
Bundling | Convex Developer Hub
Bundling is the process of gathering, optimizing and transpiling the JS/TS
Michal Srb
Michal Srb•14mo ago
(so to your question there's no resolveJsonModule in convex.json - if its TypeScript complaining make sure you're changing the right tsconfig.json, our templates usually have two, one inside the convex/ folder)
ian
ian•14mo ago
I made a thread in #stack for the new article: https://discord.com/channels/1019350475847499849/1195812243892355192

Did you find this page helpful?