DenisBD
Convex Community9mo ago
12 replies
DenisB

How to use Zod for validating database writes?

I read in https://stack.convex.dev/typescript-zod-function-validation that it is possible to define a Zod schema and validate all database writes/updates using Zod. The article mentioned that, in order to do this, ctx.db must be wrapped. Does anyone have an example of how to do this? I tried to understand how rowLevelSecurity works (as it was mentioned in the article), but it seems to be beyond my comprehension.

For example, I want the field of an object in db be validated like z.number().int().gt(0).safe()
Use Zod with TypeScript for argument validation on your server functions allows you to both protect against invalid data, and define TypeScript types ...
Zod with TypeScript for Server-side Validation and End-to-End Types
Was this page helpful?