Clubbing .parse and .refine errors/issues in v3 #437
-
We have declared a zod object schema with
What is happening in v3 is that only when the criteria laid down by |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I understand the need for something like this, but it's not likely in the short term. Zod doesn't catch errors that are thrown inside of refinements, so passing an invalid value into a refinement function is likely to throw an unexpected error. Zod schemas validate types first, then pass that value through a chain of refinements/transformations. Passing invalid data into this chain is not the intended behavior. The "right" way to solve this is to differentiate between fatal and non-fatal validation issues. That way if there is a minor validation issue "inside" an object, we could still run the outer refinements. z.object({
name: z.string().min(5, { nonfatal: true }),
}).refine(val => /* run this even if string is too short */) But this is a major refactor and isn't likely anytime soon. Open to other solutions as well. |
Beta Was this translation helpful? Give feedback.
I understand the need for something like this, but it's not likely in the short term. Zod doesn't catch errors that are thrown inside of refinements, so passing an invalid value into a refinement function is likely to throw an unexpected error. Zod schemas validate types first, then pass that value through a chain of refinements/transformations. Passing invalid data into this chain is not the intended behavior.
The "right" way to solve this is to differentiate between fatal and non-fatal validation issues. That way if there is a minor validation issue "inside" an object, we could still run the outer refinements.