Single source of truth for models using Zod and Prisma

I feel I'm redundantly defining my models using Zod and Prisma. I do a rough db-fitting definition in my Prisma schema file, and another more detailed one in my Zod schemas. I tried using prisma-zod-generator to have my Prisma schema file as a SSoT, but since I need more detailed validations like min/max for numeric values, and also more complex refinements, this solution is not good enough. How do you guys do it? Do you have any recommendations?
4 Replies
Keef
Keef11mo ago
I wouldn't shove my validation logic in there since its mostly business logic and aside from simple type checks (making sure the value is text) I think its an anti pattern to shove stuff like that into your database. I would in your situation consume the schemas generated from prisma and just super refine them further then in the application I would use the refined ones
naaaterino
naaaterino11mo ago
this is actually a good chatgpt task. it’s really good at this, and the zod objects are really easy to read so you can verify that what it gives you is good
froxx
froxx11mo ago
Thanks for the suggestion. I thought about that, but it would leave the possibility to devs to use the basic, unrefined schemas. Although I could still at least try to prevent that with some... ✨ documentation ✨ Anyway I don't know if you tried prisma-zod-generator yourself. It kind of does the job creating Zod schemas, but it has a huge amount of overhead for my purpose, and it offers little to no configuration. I wish zod-prisma was still maintained. It seems it did the job a lot better What do you mean by that? What would chatgpt be good for here? Generating the zod schemas? That would not be my problem. I don't hesitate writing the schemas myself, I hesitate having to manually change pretty much the same info twice when e.g. my model gets a new prop
naaaterino
naaaterino11mo ago
okay yeah fair enough then, idk of anything that would do what you're describing. however i'm not sure how such a library would generate zod objects with .min(5) for example, if that fact is not represented in the prisma schema. but if you find something to this effect (like maybe you define it with an object from their library?) then kindly @ me