Export InvalidUnionError type
See original GitHub issueI am currently making a utility function for a use case to specially handle union errors. The error object is a bit nested in my situation so feeling the need for that. Can InvalidUnionError
be exported as a type from the library? Otherwise finding it a bit difficult to deal with it in TS.
For instance, I have:
function findUnionError(zodError: zod.ZodError): ZodSuberror | undefined {
return zodError.errors.find(err => {
return err.code === zod.ZodErrorCode.invalid_union;
})
}
It will be great if I can do:
function findUnionError(zodError: zod.ZodError): InvalidUnionError | undefined {
return zodError.errors.find((err) => {
return err.code === zod.ZodErrorCode.invalid_union;
}) as InvalidUnionError | undefined;
}
Because later I need to access the .unionErrors
property and can’t do it:
I am again needing to do:
function something(err: zod.ZodError): void {
const errorItem = findUnionError(err);
if (errorItem !== undefined && errorItem.code === zod.ZodErrorCode.invalid_union) {
// do something with errorItem.unionErrors
errorItem.unionErrors
}
}
which feels like a little extra steps.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
export type * from 'somewhere' #48508 - GitHub
The way to think about type-only imports and exports is that they have identical resolution semantics to their non-type-only counterparts, but ...
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
As of zod@1.11
InvalidUnionError
and all other suberror data types are now exported@vriad
No, I am trying to access a deeply nested error in a
ZodSuberror
with error codeZodErrorCode.invalid_union
. I tried usingZodErrorMap
but the thing it seems to be good at is modifying the error messages. In my use case, I am not altering the error message. The issue is that the error message I am interested in extracting is quite deep:InvalidUnionError
..unionErrors
property which is an array ofZodError
..errors
array size.ZodError
, theZodSuberror
I am interested in is again aInvalidUnionError
.For achieving it, one of the utility function I made is this:
So if
InvalidUnionError
is exported natively, it will be quite useful in my case. Any other suggestion is also greatly appreciated.