saj
saj2mo ago

zod validation roadmap & philosophy

also would like to hear convex's philosophy long term around this subject, because zod's api is quite comprehensive https://zod.dev/api i'm just going through my codebase and across 200+ files referencing zod here are some random examples: z.coerce.number() z.string().min(1, { error: "Required" }).max(255) z.instanceof(File) z.string().nullable() z.string().nullish() z.record(z.string() z.unknown()).optional() z.number().int().positive().max(500).default(25)
2 Replies
ian
ian2mo ago
Hey @saj - I've written a bunch on this in other Discord threads, but the high level is: - Convex validators are at the database level and provide strong guarantees about the data at rest matching your schema. When you change v.optional(v.string()) to v.string() it guarantees that every field has a string set and can do so really efficiently. changing z.string() to z.email() would require doing a full table scan and running javascript on every row, so we will not support this any time soon. - The big risk of first-party wrapping is that it gives the illusion we're doing more than we are. the platform guarantees are around primitive data types that we can validate and serialize in all languages, not just TS. - We do argument validation in Rust and use that data for returning query cache hits without spinning up JS, so we need them to be in a format we can serialize and zod is so expressive it's too unwieldy to replicate in our own Rust version. - Yes that article's publish date is old - I've been updatig it as new features come in but it deserves more complete docs - there's also some examples in the zod tests https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/server/zod.test.ts#L37-L77 but I'd love to add better docs for helpers overall. - There's an active project to also support zod4 you might want to chime in on - it's a PR on the convex-helpers repo. - Using zod helpers for more refined validation within functions, or before writing / after reading from the DB is totally ok & you can feel free to keep using your zod validators. I made the zodToConvex and convexToZod and all the z* helpers to make this more convenient and avoid duplication.
GitHub
convex-helpers/packages/convex-helpers/server/zod.test.ts at main ...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
ian
ian2mo ago
Very open to ideas and suggestions - just open a GitHub issue

Did you find this page helpful?