zod
zod copied to clipboard
[3196] add 'mergeErrorPath' property into ZodInvalidIntersectionTypes…
PR for issue #3196
Currently ZodInvalidIntersectionTypesIssue does not return enough information about occurred error. We can pass object/array of big size and depth into z.intersection(...).parse() function, but we will not get information what exact property is a reason of merge failure. This behavior makes it difficult to debug and find the field which cause of the error.
I want to propose additional property mergeErrorPath in ZodInvalidIntersectionTypesIssue which will contain path to field that can not be merged. This way, library users will be able to see exactly where the problem is and solve it much faster and easier.
const schema = z
.strictObject({
property: z
.strictObject({
nested1: z.function(),
nested2: z.number()
})
.optional()
})
.and(z.custom((value) => !(value instanceof RegExp)));
const result = schema.parse({
property: {
nested1: () => {},
nested2: 1000
}
});
// Behavior before PR
{
code: 'invalid_intersection_types',
path: [],
message: 'Intersection results could not be merged'
}
// Behavior after PR
{
code: 'invalid_intersection_types',
path: [],
mergeErrorPath: ['property', 'nested1']
message: 'Intersection results could not be merged'
}
Deploy Preview for guileless-rolypoly-866f8a ready!
Built without sensitive environment variables
| Name | Link |
|---|---|
| Latest commit | 87402e02fd8b3fe0fc38750a36c16f15d4a71234 |
| Latest deploy log | https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/65bbcb89b93adc0008a20f63 |
| Deploy Preview | https://deploy-preview-3208--guileless-rolypoly-866f8a.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
Thanks! Great PR. I think this is a good step towards better error reporting for intersections so I'm merging it into the v4 branch.
That said, this is likely to undergo some changes before actually landing in Zod 4. I think it would be neat if Zod reported separate issues for each merge failure, instead of eagerly aborting after the first failure. But this is a really good start.