Extend zod schema with prisma.schema comments
olehmelnyk opened this issue · comments
Problem
Since Prisma does not have such a reach check as Zod has - like min/max/length, email/URL, transform, etc. - we could add those extra Zod checks using Prisma comments ///
prisma.schema
model Post {
id String @id @default(uuid()) /// @zod.uuid()
/// @zod.max(255, { message: "The title must be shorter than 256 characters" })
title String
contents String /// @zod.max(10240)
}
zod schema output
export const PostModel = z.object({
id: z.string().uuid(),
title: z.string().max(255, { message: 'The title must be shorter than 256 characters' }),
contents: z.string().max(10240),
})
Suggested solution
here's an example in a similar prisma2zod generator
Just a suggestions
Maybe you can optionally provide an npm package, that generates tRPC based on zod schema from another plugin(s).
Or maybe in generator configuration, we can add pre- and post-processors to be triggered during prisma.schema generation to do some extra stuff.
Hello @olehmelnyk
While this might be a nice addition, it's not going to be as simple. The case of zod-prisma
generator is simple because only base models schemas are generated; like Post
and User
. Here it's different, full CRUD schemas get generated. So the expected solution should cover all possible paths deep down. Contributions are welcomed of course, so you can give it a try if you would like.
Could you provide examples on the suggestions?
Like an example on zod-to-trpc
and an example on the pre
and post
hooks. I want to understand what you have in mind. Maybe one of them can be done.
Thank you for using this generator :)
Sure.
prisma-to-trpc generator currently depends on prisma-to-zod generator.
Since your prisma-to-zod generator does not support extra zod-validations via comments, and another project has that functionality (zod-prisma) - I thought it would be nice if we can run our custom middlewares/hooks before and after prisma-to-trpc is generated.
Here's an example:
- we update prisma.schema
- we run prisma generate to create migrations
- prisma-to-trpc generator (or maybe new one generator with pre- and post-middlewares) runs, so we could run custom code before and after trpc is generated.
Like:
- generate zod.schema with extra validations from
///
prisma comments (ex. zod-prisma) - generate tRPC handlers for the backend using zod.schema from the previous step (that's what this repo is responsible for)
- how about generating tRPC handlers for the frontend as well?
- I can imagine we could even generate front-end forms since we know the fields, their types, and validations - of course, this would not be ideal, but just a good start for prototyping - so those forms could be used in the client-side app and admin dashboard (probably will have to write a custom module for this, once I figure out how those generators works)
- we can generate mocks for testing based on zod.schema (ex. zod-mocking)
- generate DB seeds based on zod.schema and faker (ex. @anatine/zod-mock)
- generate OpenAPI docs from zod.schema (ex. @anatine/zod-openapi)