Catch all errors for nested schemas and only return singular error #961
Unanswered
jordansoltman
asked this question in
Q&A
Replies: 1 comment
-
Try this. Let me know if you have any questions. const schema = z.object( {
options: z.custom( value => {
return z.object( {
max_length: z.number().optional(),
min_length: z.number().optional(),
} ).optional().safeParse( value ).success
}, {
message: 'invalid options',
} )
} )
const result = schema.safeParse( { options: { max_length: NaN } } )
console.log( !result.success && result.error.message )
// [
// {
// "code": "custom",
// "message": "invalid options",
// "path": [ "options" ]
// }
// ] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When parsing this object, is there a way to capture all of the errors for a certain schema, and return them as a singular error. For example, for the options property, if there is an error with max_length, or hypothetically any of the other properties on that object, I want to return a singular error that says "invalid options" regardless of what the actual error may be.
Any ideas?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions