#Using the validator for validation
4 messages · Page 1 of 1 (latest)
Hi @marble kindle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, it is possible to use the validator functions exported by Convex for validation. However, the validation process in Convex is slightly different from the way it's done in Zod.
In Convex, you define validators using the v object. For example, you can define a validator for a user object like this:
const userValidator = v.object({
name: v.string(),
});
This validator can be used in schema definitions and as input validators for Convex functions (source (https://docs.convex.dev/api/modules/values#v)).
However, the validation process is not explicitly done by the developer. Instead, Convex uses these validators to automatically validate the data when it's used in Convex functions or schemas. For example, if you define a Convex function with arguments that should match your schema definitions, Convex will automatically validate the arguments using the validators you defined (source (https://stack.convex.dev/types-cookbook#keep-validators-from-going-stale)).
If you want to manually validate data like you would do with Zod's safeParse method, you might need to use a different approach. One of the support staff in a Discord thread mentioned that the Rust implementation that powers argument and schema validation in Convex is not exposed to JS. However, they suggested that you could use the convex-test library, which has a pure-JS implementation of the validation, or use Zod and map from Zod to Convex validators with convex-helpers (source (https://discord.com/channels/1019350475847499849/1244925890803269708)).