In Zod, z.enum
helps you restrict inputs to a predefined set of string values—nothing more, nothing less.
Think of it like a dropdown: users must choose from the allowed options you define.
For example, z.enum(["draft", "published", "archived"])
ensures only those exact strings are valid.
It automatically infers a union type and provides helpful error messages when validation fails.
This is ideal for user roles, statuses, or any case where the value must match one of a few specific strings.
Defining and Parsing Enum in Zod
import { z } from "zod";
const StatusSchema = z.enum(["draft", "published", "archived"]);
StatusSchema.parse("draft"); // Valid
StatusSchema.parse("Draft"); // Error: not one of the allowed values
StatusSchema.parse("deleted"); // Error: not one of the allowed values
The enum is case-sensitive and does not allow any value outside the provided list. This ensures that only "draft"
, "published"
, or "archived"
are accepted—making your data clean, predictable, and safe.
But that’s not the only way to define enums in Zod.
Zod offers multiple ways to create and validate enums depending on your data structure or development style. You can:
- Create enums from an array of strings
- Generate enums from object keys or object values
- Use existing TypeScript enums
- Define enums from TypeScript union types
Enum Error Handling in Zod
Zod provides powerful tools to handle errors gracefully when working with enums. While the default error messages are helpful, customizing them ensures a better developer and user experience. Whether you’re validating user roles, statuses, or fixed options, you can fine-tune error handling to match your application’s tone and clarity needs.
There are two main ways to handle error messaging in Zod enum validation:
- Generic Error Messages – Apply a single, static message for all invalid values. Ideal for simple use cases where one friendly message is enough.
- Varying Custom Messages Using
errorMap
– Define dynamic, context-aware messages based on the input or error type. This is perfect for delivering precise feedback tailored to each validation failure.
Enum Utility Methods in Zod
Zod provides two helpful utility methods to refine enums: exclude()
and extract()
. These methods allow you to create subsets of an existing enum schema without redefining it manually.
exclude()
removes one or more values from the original enum. It’s useful when you want to filter out certain options, such as disallowing “guest” in restricted user roles.extract()
lets you pick specific values from the enum. This is ideal when you only want to allow certain roles, statuses, or categories in a specific context.
These methods are especially valuable in real-world scenarios like:
- Restricting access to admin-only features by excluding public roles
- Creating a validation schema that allows only publishable content states
- Dynamically tailoring enum schemas based on form flows or API routes
By using exclude()
and extract()
, you keep your code DRY, improve maintainability, and ensure consistent validation across your app.
Conclusion
Zod’s z.enum
gives you precise control over string-based validation by enforcing a strict set of allowed values. Whether you’re validating content statuses like "draft"
and "published"
, user roles such as "admin"
and "viewer"
, or any other fixed choices, z.enum
ensures only those exact values pass.
You can create enums from string arrays, object structures, or TypeScript types, and easily refine them with utility methods like extract()
and exclude()
—perfect for tailoring validations in different parts of your app.
Combined with custom error messages using errorMap
, Zod enums let you deliver accurate, user-friendly validation while keeping your schemas consistent and maintainable across your codebase.