Skip to content
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

Closed
cjoecker opened this issue Oct 31, 2023 · 7 comments
Closed

nativeEnum invalid_type_error not returning error on wrong enum #2919

cjoecker opened this issue Oct 31, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@cjoecker
Copy link

cjoecker commented Oct 31, 2023

Zod returns Invalid enum value although invalid_type_error is set.

Steps to reproduce

const schema = z.nativeEnum({MY_ENUM:"MY_ENUM"}, {
  invalid_type_error: "this is an error",
})

console.log("result", schema.parse("AAA"));
// returns error "Invalid enum value. Expected 'MY_ENUM', received 'AAA'"

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:

  1. Add in the nativeEnum documentation section what is invalid_type_error for
  2. Add with JS Docs what is invalid_type_error for
  3. Add an invalid_enum_error to the params of nativeEnum where we can define a custom message.
@cjoecker cjoecker changed the title nativeEnum invalid_type_error not returning error on empty string nativeEnum invalid_type_error not returning error on wrong enum Oct 31, 2023
@JacobWeisenburger
Copy link
Contributor

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! 🙏
https://github.com/sponsors/JacobWeisenburger

@cjoecker
Copy link
Author

cjoecker commented Nov 1, 2023

Hi @JacobWeisenburger, thanks for your answer.
This issue is not about if it's possible to show a custom type error with errorMap
It is about how the nativeEnum helper is built since it is misleading that there is an invalid_type_error that is not documented and does not work as I intuitively would expect. So, I wrote in the points above what I would expect from the helper.

@JacobWeisenburger
Copy link
Contributor

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.

@JacobWeisenburger JacobWeisenburger added enhancement New feature or request and removed move-to-discussions? labels Nov 1, 2023
@jrmyio
Copy link

jrmyio commented Nov 15, 2023

Also strumbled on this.

Thought I customized all the errors until I found out invalid_type_error isn't actually applied when a string or number is passed that isn't part of the enum. If you pass null, objects, arrays you still get your specified invalid_type_error.

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:
https://codesandbox.io/s/zod-enum-test-t3s4dc?file=/src/index.tsx

@fernandollisboa
Copy link
Contributor

I can work on that! @JacobWeisenburger

@colinhacks
Copy link
Owner

You can now specify this error message with the message field #3169

@mv-joaopaulo
Copy link

This implementation hasn't fixed all scenarios, the message overwrites even the required error.
If the value is undefined, it should return the required error message.
If the value is defined but invalid, it should return the invalid error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants