gwilliamnn
gwilliamnn•2y ago

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
Michal Srb
Michal Srb•2y ago
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.
gwilliamnn
gwilliamnnOP•2y ago
Let me check this
ian
ian•2y ago
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...
ian
ian•2y ago
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.
gwilliamnn
gwilliamnnOP•2y ago
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,
ian
ian•2y ago
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 🙏
gwilliamnn
gwilliamnnOP•2y ago
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...
ian
ian•13mo ago
Update: you can use this helper from npm i convex-helpers: https://stack.convex.dev/typescript-zod-function-validation
Using 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 ...

Did you find this page helpful?