Skip to content

z.enum() breaks if you pass it an actual enum or array created by destructuring enum using Object.values()` #2125

Answered by JacobWeisenburger
rogadev asked this question in Q&A
Discussion options

You must be logged in to vote

Is this what you are looking for?
https://github.com/colinhacks/zod#native-enums

enum PizzaType {
    Canadian = 'Canadian',
    Cheese = 'Cheese',
    Hawaiian = 'Hawaiian',
    MeatLovers = 'Meat Lovers',
    Pepperoni = 'Pepperoni',
    Vegetarian = 'Vegetarian',
}

const orderSchema = z.object( {
    id: z.number(),
    type: z.nativeEnum( PizzaType )
} )

const order = {
    id: 1,
    type: 'Canadian'
}

orderSchema.safeParse( order )

Other Resources:
https://gist.github.com/JacobWeisenburger/1c72eda9e823cd6caed327db470f0613

If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏
https://github.com/sponsors/Jac…

Replies: 6 comments 9 replies

Comment options

You must be logged in to vote
2 replies
@silent-tan
Comment options

@fnandogp
Comment options

Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
2 replies
@JacobWeisenburger
Comment options

@rogadev
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@lorenzoa7
Comment options

@andredezzy
Comment options

@mariuslundgard
Comment options

@scotttrinh
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@AbubakarWebDev
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #2124 on March 02, 2023 00:01.