Do you manually create Zod schemas for your Prisma models?

I found myself doing this and thought there should be a better way, cause I have to make the same change in two places. I then googled "prisma zod" saw someone created this, which trys to solve this (I still yet to try it): https://github.com/CarterGrimmeisen/zod-prisma. I was also wondering if Drizzle possibly has this "one source of type truth" solution built in?
8 Replies
alexmartos
alexmartos8mo ago
Does not seem that Drizzle have that built in but as a plug-in: https://orm.drizzle.team/docs/zodiac
George
George8mo ago
You can create zod schemas from your drizzle schemas like shown here: https://orm.drizzle.team/docs/zod
Usage - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
cadams
cadams7mo ago
this DX seems amazing, thanks for the share @G3orge
shikishikichangchang
It’s difficult to extend the schema tho. Say if you wanna add a custom message for each field
François Best
François Best7mo ago
I've used (and contributed to) Carter's tool, it's indeed a neat way to keep the schema as a source of truth. Turning the resulting Zod schemas into JSON Schemas helped turn a Fastify server into a tRPC-like experience.
Max
Max7mo ago
@cadams if i were to start today i would go with drizzle, that package that @G3orge shared looks great. I just saw this package, haven't used it. Might tinker and checkout next week. https://www.npmjs.com/package/zod-prisma
LittleLily
LittleLily7mo ago
There's a second argument to createSelectSchema/createUpdateSchema that lets you extend/refine the definition of each field to add stuff like min/max bounds, extra validation like email/url/regex, and you can put custom messages in there too if you want It's also good for adding openapi metadata for zod-openapi
shikishikichangchang
ah I see, thanks for that info! I was using that at first and didn't like how I couldn't customize the message. But now I know
Want results from more Discord servers?
Add your server
More Posts
Zod validation with react-hook-formIs there a way to validate form field for multiple errors simultaneosly? My use case is password stHaving an issue when using Nextauth for signing in getting `Invalid Compact JWE`So i have adjusted my User interface and what Nextauth session return. this is my new interfaces: ``Using tRPC without the back-end lock-in. I like it now, but wanna be flexible in the future.Hey, everyone! My use-case here is that I'm using NextJS for a project that relies on somewhat complIs there a way to get a previous dynamic route in app directory?Take this for example ```ts app - site - [slug] - map - page.tsx // how to get tRPC alternatives for a simple public APII have a T3 monorepo with tRPC with 100+ PRIVATE API endpoints. 1) I want to create a public API wii am building a react component library how can i keep the bundle size very lowI am trying to build a component library for react Nothing to crazy just a simple and sing componentBest way to prefetch queries with tRPC and app/ dirI am in the process of switching my pages/ T3 app to the new fancy app directory and know that ReactonSuccess, onError and onSettled deprecated next major version?with `onSuccess`, `onError` and `onSettled` being deprecated soon™️, what would the best practice beHow did Vercel Implement this ??When you encounter an error, it opens up the editor file in VSCode. How do you built this browser feHave someone implemented pagination with server actions and prisma?Could you send link if so