-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nativeEnum invalid_type_error not returning error on wrong enum #2919
Comments
Is this what you are looking for? const schema = z.nativeEnum( { MY_ENUM: 'MY_ENUM' }, {
errorMap: ( issue, ctx ) => {
if ( issue.code === 'invalid_enum_value' ) {
return { message: 'this is an error' }
}
return { message: issue.message ?? '' }
},
} )
const result = schema.safeParse( 'AAA' )
!result.success && console.log( result.error.issues )
// [
// {
// received: "AAA",
// code: "invalid_enum_value",
// options: [ "MY_ENUM" ],
// path: [],
// message: "this is an error"
// }
// ] If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Hi @JacobWeisenburger, thanks for your answer. |
Thanks for your kind words. I was just trying to see if you are requesting a feature enhancement or just looking for a work around. |
Also strumbled on this. Thought I customized all the errors until I found out Is is correct to assume that zod always exposes all possible error codes for a z.type in the options? If so this is more a bug than an enhancement. Here is a sandbox to test this: |
I can work on that! @JacobWeisenburger |
You can now specify this error message with the |
This implementation hasn't fixed all scenarios, the message overwrites even the required error. |
Zod returns
Invalid enum value
althoughinvalid_type_error
is set.Steps to reproduce
Expected behavior
When the type is wrong, Zod should return whatever I give in
invalid_type_error
.In the example, it should return
this is an error
If
invalid_type_error
is meant to be used for something else, I would expect the following:nativeEnum
documentation section what isinvalid_type_error
forinvalid_type_error
forinvalid_enum_error
to theparams
ofnativeEnum
where we can define a custom message.The text was updated successfully, but these errors were encountered: