Detailed errors for union schema
See original GitHub issueI’d like to have more information or even TS-like details for union errors (Add a flag to parse?):
My types:
export const T = z.union([
A, B, C
])
So what I currently get from this is:
Error: 1 validation issue(s)
Issue #0: invalid_union at
Invalid input
What I’d like to get is something more similar to this:
'{ t: 1 }' is not assignable to parameter of type '{ a: number } | { b: number }'
Is this sensible|doable|easy|hard?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Detailed errors for union schema #117 - colinhacks/zod - GitHub
I'd like to have more information or even TS-like details for union errors (Add a flag to parse?): My types: export const T...
Read more >Handling GraphQL errors like a champ with unions and ...
Error handling can be frustrating in GraphQL. This post shows you how to use unions and interfaces to handle errors in a more...
Read more >GraphQL Error Handling with Union Types - Episode #30
The GraphQL Spec briefly discusses error handling in its simplest form for both requests, and field, but if you're used to working with ......
Read more >Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >GraphQL mutation errors (union errors with interface aka 6a ...
This article tries to show the concrete implementation of stage 6a mutation error handling from Marc-Andre Giroux Guide to graphql errors under ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems like this could be improved with a union resolver function as I proposed in https://github.com/vriad/zod/issues/100#issuecomment-667584554. As long as the input matches well enough (e.g. with a discriminator field) to be recognized as an element of the union in userland, the errors could be simplified to a single case instead of needing to resolve
unionErrors
within aninvalid_union
error type.So here, parsing
input
with into either theunion
orintObj
schemas should result in:Simple answer:
You have access to all this error information already. Zod throws a special subclass of Error called
ZodError
that contains detailed error information in the.errors
property:This wil print:
More complicated answer:
Error handling in Zod is complex enough that I split it into it’s own README: https://github.com/vriad/zod/blob/master/ERROR_HANDLING.md
That guide explains how to do this. Getting the exact error information you need can feel a bit like spelunking, but I’m pretty sure this complexity is irreducible. Believe me, I’ve tried 😃
Here’s how to do what you want:
import * as z from ‘.’;
If you don’t want to write this error handling code every time you want to handle union errors you should write your own Error Map which lets you customize the message for every kind of error in Zod. You then pass your error map as a parameter to .parse(). Instructions for doing so are all explained in the guide. 👍
You can get a sense for all the error data available to you like this: