Can I import the Type from schemas?
How can I have the schema types, and if have also a validation schema like zod.
This is good when we create forms and also tables to have the schema and or the return type.
8 Replies
Schema types documentation: https://docs.convex.dev/typescript#adding-a-schema
You can use zod inside your mutation for more granular validation.
TypeScript | Convex Developer Hub
Move faster with end-to-end type safety.
Let me check this
Also if you want to use Zod types instead of our input validation, you can see here: https://stack.convex.dev/wrappers-as-middleware-zod-validation#using-withzod-for-input-validation
note: that post is a bit old, but should still stand up.
Zod Validation: Wrappers as “Middleware”
Function validation is important for a production app because you can’t always control which clients are talking to your server. See how to use zod to...
And a working demo: https://github.com/get-convex/convex-demos/blob/main/zod-validation-ts/convex/messages.ts
GitHub
convex-demos/zod-validation-ts/convex/messages.ts at main · get-con...
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
Very nice...! I know is just the beggining (version 1), but a generator function to auto generate zod types will be good also, maybe a community plugin,
I like that idea. I'm curious how you'd want to extend them. Out of the box, you'd get something like a zod object with nested zod validators. But you probably want to update the nested validators to be more specifically for email, etc. If you have a specific syntax example in mind we can consider it if we look to do this in the future 🙏
yeah, I use sanity, and they use groq query...
and this library is a thrid party library, use zod to get back the the type and validation...
Update: you can use this helper from
npm i convex-helpers
: https://stack.convex.dev/typescript-zod-function-validationUsing Zod with TypeScript for Server-side Validation and End-to-End...
Use Zod with TypeScript for argument validation on your server functions allows you to both protect against invalid data, and define TypeScript types ...