Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend zod schema with prisma.schema comments #14

Open
olehmelnyk opened this issue Jul 15, 2022 · 2 comments
Open

Extend zod schema with prisma.schema comments #14

olehmelnyk opened this issue Jul 15, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@olehmelnyk
Copy link

olehmelnyk commented Jul 15, 2022

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.

@omar-dulaimi omar-dulaimi added the enhancement New feature or request label Jul 16, 2022
@omar-dulaimi
Copy link
Owner

omar-dulaimi commented Jul 16, 2022

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 :)

@olehmelnyk
Copy link
Author

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:

  1. we update prisma.schema
  2. we run prisma generate to create migrations
  3. 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants