Validate from an array of possible literal values #500
MikeRippon
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
|
As long as the enum represents a string value, z.enum() should do the trick. Does that help? enum Fruit {
Strawberry = "Strawberry",
Apple = "Apple",
Kiwi = "Kiwi",
Orange = "Orange",
Lime = "Lime",
Lemon = "Lemon",
};
const citrusFruits = [Fruit.Lime, Fruit.Lemon, Fruit.Orange] as const;
const citrusFruitsSchema = z.enum(citrusFruits) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, this can be done with
refine()andtransform(), but I'm just wondering if there is a shorter syntax for validating based on an array of literal values. It seems like it should be a reasonably common use case? Chainingorisn't really suitable if the array of values is declared and used elsewhere in the codebase.My current situation is based on enums, but I don't see why this would have to be enum-specific, and could potentially work with any value:
Would be nice:
Current workaround
Sorry if I've missed something in the docs!
Beta Was this translation helpful? Give feedback.
All reactions