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

Using the key of string enum using nativeEnum? #3617

Open
hls-app opened this issue Jul 3, 2024 · 2 comments
Open

Using the key of string enum using nativeEnum? #3617

hls-app opened this issue Jul 3, 2024 · 2 comments

Comments

@hls-app
Copy link

hls-app commented Jul 3, 2024

So I have been using this Country enum and wondering if we can validate the keys instead of the value?

https://gist.github.com/evolkmann/740d24889c509c08484a8ff72af5dd64

Currently, the nativeEnum validates the value and could not find an option to make it validate the key instead.

@hls-app
Copy link
Author

hls-app commented Jul 6, 2024

Currently using this as I cannot flip the enum just for Zod

z.string().length(2).regex(/^[A-Z]{2}$/, 'Invalid country code').nullable()

@nabil-rady
Copy link

nabil-rady commented Jul 6, 2024

This how I did it

z.enum(Object.keys(Enum) as [keyof typeof Enum, ...(keyof typeof Enum[]])

Let me know if you have a better idea than this.

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

No branches or pull requests

2 participants