chrishoermann / zod-prisma-types

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

[BUG] Regex failing for e.164 phone number format #225

Open timfenton-dermi opened 6 months ago

timfenton-dermi commented 6 months ago

Describe the bug Attempting to use custom validator for validating a phone number is in e.164 format

phoneNumber            String? /// @zod.custom.use(z.string().regex(/^\+[1-9]\d{1,14}$/));

this pattern is breaking the generation of the zod regex check:

 phoneNumber: z.string(.nullable(),

expected output:

 z.string().regex(/^\+[1-9]\d{1,14}$/)).nullable()

Package versions (please complete the following information):

seems to be related to the escaped characters + and \d

chrishoermann commented 6 months ago

@timfenton-dermi Thanks for pointing this out. I'll look into it

lellimecnar commented 1 month ago

The only way I was able to get around this was to export my regex from an external file and add it to the @zod.import(...):

@zod.import(["import * as u from '.......';"])

...and do this:

/// @zod.custom.use(z.string().regex(u.PHONE_REGEX))

...though in doing that, I also discovered that imports only work for models, and not types... is that correct?