omar-dulaimi / prisma-zod-generator

Prisma 2+ generator to emit Zod schemas from your Prisma schema
MIT License
513 stars 45 forks source link

Extra validation on generated output - e.g minlength of string #80

Open outofthisworld opened 1 year ago

outofthisworld commented 1 year ago

Bug description

Given a user model like

model User{
   email string
}

produces the generated class

const UserCreateOneSchema = z.object({ email: z.string() });

However, using validation against this schema on a formSubmit coerces undefined into an empty string, or inputs with no data become empty strings and this validation succeeds.

Ideally email isn't: ''

Would be handy to have some sort of way of specify minLength

How to reproduce

  1. validate using schema in a form onsubmit method

Expected behavior

  1. Some way of specifying validation requirements

Prisma information

Prisma v4

Environment & setup

macOs

Prisma Version

4

TalissonBento commented 1 month ago

up 👍

TalissonBento commented 1 month ago

I suggest something like this:

model User{
   email string /// @zod.min(3, 'too short').max(18, 'too long')
}