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

add custom error message on nativeEnum validation #3158

Conversation

fernandollisboa
Copy link
Contributor

@fernandollisboa fernandollisboa commented Jan 18, 2024

Resolves #2919

Return invalid_type_error on nativeEnum validations instead of Invalid enum value when the attribute is set.

const schema = z.nativeEnum({ MY_ENUM: "MY_ENUM" }, {
  invalid_type_error: "the value provided is invalid",
})

console.log("result", schema.parse("AAA"));
// returns error "the value provided is invalid"

Copy link

netlify bot commented Jan 18, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 9bd71f3
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/65d44426557a55000894160a
😎 Deploy Preview https://deploy-preview-3158--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@fernandollisboa fernandollisboa changed the title Feature/add custom error message on enum validation add custom error message on enum validation Jan 18, 2024
@fernandollisboa fernandollisboa changed the title add custom error message on enum validation add custom error message on nativeEnum validation using invalid_typ-error Jan 18, 2024
@fernandollisboa fernandollisboa changed the title add custom error message on nativeEnum validation using invalid_typ-error add custom error message on nativeEnum validation Jan 18, 2024
@fernandollisboa fernandollisboa force-pushed the feature/add-custom-error-message-on-enum-validation branch from 404f2c1 to 4e60561 Compare January 18, 2024 23:07
Copy link

@joaoGabriel55 joaoGabriel55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice tests!

@fernandollisboa fernandollisboa force-pushed the feature/add-custom-error-message-on-enum-validation branch from 4e60561 to 5a45625 Compare February 20, 2024 06:10
@scarabcoder
Copy link

Is there an update on this?
Currently working with a list of US States as a native enum, having Zod throw out an error "Invalid string, expected one of <list of all 50 states>" doesn't make for the greatest user experience.

@fernandollisboa
Copy link
Contributor Author

@scarabcoder i believe they're working on a fix to be released on Zod 4: #3146 (comment)

@colinhacks
Copy link
Owner

My preferred solution here is the one that landed in #3169, which will land in Zod 3.23 shortly.

This is all liable to change in Zod 4 as well.

@colinhacks colinhacks closed this Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nativeEnum invalid_type_error not returning error on wrong enum
4 participants