Closed olehmelnyk closed 10 months ago
Hey @olehmelnyk , ZenStack already has a built-in feature called field validation: https://zenstack.dev/docs/reference/zmodel-language#field-validation
Is it something you're looking for?
@ymc9 not really, Current prisma-to-zod can generate only basic zod validations - such as type (string/number/boolean) if the field is required and probably that's it... zod allows us to make even more advanced validations - like min/max length, regexp checks, transformation, validate one field based on another, etc. However, it's not possible to generate those additional checks from the Prisma schema. So there was another prisma-to-zod validation plugin created, that allows adding those additional zod-checks via Prisma comments ///
check this example - https://www.npmjs.com/package/zod-prisma-types#field-validators - this is the generator/plugin that works with native prisma. I want to work with ZenStack.
Hi @olehmelnyk , I might have some misunderstanding here, but my current feeling is what ZenStack's field validation feature does is pretty close to "zod-prisma-types"'s field validators, but with syntax native to ZModel. It supports basic type validation (min/max, regex, email, url, etc.), as well as custom refinement).
A sample usage looks like:
model Signup {
id Int @id @default(autoincrement())
name String
email String @email @endsWith("@zenstack.dev", 'Must be a @zenstack.dev email')
adult Boolean
beverage String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@allow('create,read', true)
@@validate(beverage in ['SODA', 'COFFEE', 'BEER', 'COCKTAIL'], 'Please choose a valid beverage')
@@validate(adult || beverage in ['SODA', 'COFFEE'], 'You must be an adult to drink alcohol')
}
Here's a blog post explaining a usage scenario: https://zenstack.dev/blog/form-validation
If you have a concrete validation to implement, I can help check if it's possible with the current feature set.
Closing for now
Is your feature request related to a problem? Please describe. Can we extend zod generation with custom rules like this library - https://www.npmjs.com/package/zod-prisma-types#field-validators does with /// comments