chrishoermann / zod-prisma-types

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

Allow coercing types other than Date #237

Open RuntimeRascal opened 5 months ago

RuntimeRascal commented 5 months ago

Is your feature request related to a problem? Please describe. A way to coerce types such as Int from a Prisma schema. I am very new to both Prisma and Zod (first time using either) and seams the way to use a html input (which works with string) to represent a number type and db value type is to coerce it.

Describe the solution you'd like Would expect to use the following in a Prisma schema.

    /// @zod.coerce.number.min(1, { message: 'Number must be greater than zero'})
    someNum                 Int

Describe alternatives you've considered Very tedious to write onChange handlers to coerce things manually.

Additional context Error message [@zod generator error]: 'coerce' is not a valid validator type.

roman-chivo commented 5 months ago

Any update on this? I also need that functionality

chrishoermann commented 1 month ago

@RuntimeRascal, @roman-chivo thanks for the request and sorry for the late response. :wink:

In your case you can use custom validators like

    /// z.custom.use(z.coerce.number.min(1, { message: 'Number must be greater than zero'}))
    someNum                 Int

this sould result in

 someNum: z.coerce.number.min(1, { message: 'Number must be greater than zero'})

hope this helps.