#zod validator for schema
3 messages · Page 1 of 1 (latest)
Hi @cosmic jackal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can partially use Zod, but not directly as Convex schema validators.
Convex schemas must be defined with Convex’s own v validators (v.string(), v.object(), etc.). [Validators]
However, there is an official pattern (via convex-helpers) to convert Zod schemas into Convex validators:
-
For function arguments, you can define Zod schemas and then use helpers like
zCustomQuery/zQuerywhich:- Convert Zod → Convex validator with
zodToConvexso Convex can validate and type your function. - Run the Zod validation at runtime for stricter checks (e.g.
z.string().email()). [Zod server args]
- Convert Zod → Convex validator with
-
For database schemas, you can use
zodOutputToConvexto turn a Zod schema into a Convex validator and then pass that intodefineTable. This works, but the docs explicitly warn that:- Convex only enforces the
v-level type (e.g.v.string()), not the extra Zod refinements like.email()or.min(8). - So the stored data is guaranteed to match the Convex validator, but not necessarily all your Zod constraints, unless you also validate with Zod before writes / on reads. [DB via Zod]
- Convex only enforces the
So:
- Yes: you can define Zod schemas and convert them into Convex validators for both function args and tables using
convex-helpers. - But: Convex still needs its own
vvalidators under the hood, and Zod’s extra constraints are only enforced where you explicitly run the Zod validation.