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
alexmartos13mo ago
Does not seem that Drizzle have that built in but as a plug-in: https://orm.drizzle.team/docs/zodiac
George
George13mo 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
cadams12mo 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 Best12mo 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
Max12mo 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
LittleLily12mo 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