ZodError[] doesn't contain error from refine()
or superRefine()
#2971
-
Hi there ✌🏾, Basically, I have set up my schema for Everything is working fine until I have 2 errors not coming from the Take a look at the logging of the errors (don't pay attention to the double rendering): 1st step: I have only 2 errors, both not coming from 2nd step: I add another not-from 3rd step: I add the 4th step: I solve the 5th step: I add the Here's my schema: export const ZodProduct = z
.object({
name: z.string({required_error: 'Please, set a name.'}).min(1),
images: z.object({
mainImage: z.string().min(1, 'Please, set a main image.'),
secondaryImages: z.array(z.string().min(1)).optional(),
}),
price: z
.number({required_error: 'Please, set a price.'})
.min(0, 'The price cannot be negative.'),
comparedAt: z.number().min(0, 'The compared price cannot be negative.'),
weight: z.number().min(0),
width: z.number().min(0),
height: z.number().min(0),
length: z.number().min(0),
sku: z.string().min(1),
})
.partial({
comparedAt: true,
weight: true,
width: true,
height: true,
length: true,
sku: true,
})
.superRefine((data, ctx) => {
if (data.comparedAt && data.comparedAt <= data.price) {
ctx.addIssue({
type: 'number',
code: 'too_small',
minimum: data.price,
inclusive: false,
path: ['comparedAt'],
message: 'The compared price should be higher than the actual one.',
})
}
}) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I can't seem to understand what the problem is. Please explain what is not working correctly and produce test data that causes the problem. |
Beta Was this translation helpful? Give feedback.
-
As stated in the post, the issue is that the error triggered by If the In the screenshots from the logs, at no. 3 the |
Beta Was this translation helpful? Give feedback.
-
Please send a full reproducible code example. |
Beta Was this translation helpful? Give feedback.
-
Alright. Take this schema:export const ZodProduct = z
.object({
name: z.string({required_error: 'Please, set a name.'}).min(1),
images: z.object({
mainImage: z.string().min(1, 'Please, set a main image.'),
secondaryImages: z.array(z.string().min(1)).optional(),
}),
price: z
.number({required_error: 'Please, set a price.'})
.min(0, 'The price cannot be negative.'),
comparedAt: z.number().min(0, 'The compared price cannot be negative.'),
weight: z.number().min(0),
width: z.number().min(0),
height: z.number().min(0),
length: z.number().min(0),
sku: z.string().min(1),
})
.partial({
comparedAt: true,
weight: true,
width: true,
height: true,
length: true,
sku: true,
})
.superRefine((data, ctx) => {
if (data.comparedAt && data.comparedAt <= data.price) {
ctx.addIssue({
type: 'number',
code: 'too_small',
minimum: data.price,
inclusive: false,
path: ['comparedAt'],
message: 'The compared price should be higher than the actual one.',
})
}
}) Now
|
Beta Was this translation helpful? Give feedback.
-
Here is a much more simplified example to better illustrate the issue: const schema = z.object( { foo: z.string() } )
.superRefine( () => { console.log( 'superRefine' ) } )
console.log( 'test1' )
schema.safeParse( {} ) // doesn't run superRefine because the z.object fails
console.log()
console.log( 'test2' )
schema.safeParse( { foo: undefined } ) // doesn't run superRefine because the z.object fails
console.log()
console.log( 'test3' )
schema.safeParse( { foo: 'bar' } ) // runs superRefine because the z.object passes
/*
console:
test1
test2
test3
superRefine
*/ If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
superRefine
,transform
, andrefine
, don't run if the prior schema fails. I'm pretty sure this is intended behavior.Here is a much more simplified example to better illustrate the issue:
If…