chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
626 stars 47 forks source link

Add support for @zod.refine and validator.js #15

Closed chrishoermann closed 1 year ago

chrishoermann commented 1 year ago

It would be nice to have support for custom refinement functions like

z.string().refine(...do something)
// would be reflected in prisma model with
myfield String /// @zod.string.refine(...do something)

This refinement function should also work on non scalar types:

z.bigint.refine(...doSomething)
// would be reflected in prisma model with
myfield String /// @zod.custom.refine(...do something)

furthermore it would be nice to have validator js capabilities in the refinement functions of strings. (https://github.com/validatorjs/validator.js) Possible ways of implementing this supporting regex validation:

z.string().refine(...do something)
// would be reflected in prisma model with
myfield String /// @zod.string.refineWithValidator(...do something)

// then validator js would be added as an import
// the user needs to make shure that the package is installed
chrishoermann commented 1 year ago

validator.js can be used when specifing useValidatorJs in client config

chrishoermann commented 1 year ago

@support for @zod.refine on scalar fields dropped in favour of @zod.custom.use(...your refine logic)

kjavia commented 1 year ago

I am using this feature for comparing start and end dates on my schema record, however it seems that the refine function is only receiving the value of the field that is being validated not the entire object.

  /// @zod.custom.use(z.date().refine(schema => (schema.start && schema.end && schema.end < schema.start), { message: 'End date cannot be before start date.'}))

schema is a date not the parent object. I am on an older version (2.4.1) due to compatibility issues. Not sure if this has been fixed in the later versions.