.refine and .regex prevent .parse from returning .default value #3696
-
I'm using zod with react-hook-form and zod-i18n-map. I'm currently running up against an issue with a field that has a There are a few issues I ran into and I was wondering if there's something that I'm doing wrong. Consider this schema: const BaseSchema = z.object({
type: z.enum('One', 'Two').default('One'),
properties: z.object({
name: z
.string()
.min(3)
.max(64)
// .regex(ALPHA_NUMERIC_REGEX, { message: 'errors.can-only-contain-alphanumeric-values' }),
// Can't use regex https://github.com/aiji42/zod-i18n/issues/65
.refine(
(val) => {
return val.match(ALPHA_NUMERIC_REGEX);
},
{
params: { i18n: 'errors.can-only-contain-alphanumeric-values' },
}
)
.default('New Object'),
}),
}); With it, the call If I comment out the refine, the call to {
"type": "One",
"properties": {
"name": "New Object"
} as I would expect. Same exact behaviour is observed when I use regex:
Positioning |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
hi @MNeverOff, in the first schema, when name is undefined, the val inside the refine has 'New Object' string and it fails because of the whitespace not matching the regex. Take a look a this playground: Zod playground with refine Same behaviour we have with .regex() Zod playground with regex So wrapping up it seems like the default value is passed to regex and refine validations Additionally, verify the enum syntax: it necessitates an array as an argument. |
Beta Was this translation helpful? Give feedback.
hi @MNeverOff, in the first schema, when name is undefined, the val inside the refine has 'New Object' string and it fails because of the whitespace not matching the regex. Take a look a this playground: Zod playground with refine
Same behaviour we have with .regex() Zod playground with regex
So wrapping up it seems like the default value is passed to regex and refine validations
Additionally, verify the enum syntax: it necessitates an array as an argument.