This page documents Zod's literal, enum, and template literal schema types. These schemas validate that inputs match specific, predefined values or patterns.
For validation of arbitrary string patterns, see String Format Validators. For union types combining schemas, see Union and Discriminated Unions.
Zod provides three related schema types for validating against fixed sets of values:
| Schema Type | Purpose | Example |
|---|---|---|
| Literal | Single constant value or array of values | z.literal("red") or z.literal(["a", "b"]) |
| Enum | String or numeric enum with ergonomic API | z.enum(["red", "green", "blue"]) |
| Template Literal | TypeScript template literal patterns | z.templateLiteral(["id_", z.string()]) |
All three types leverage the values internal property to store their allowed values, which is used for optimizations like discriminated union fast paths and record key validation.
Sources: packages/docs/content/api.mdx112-155 packages/docs/content/api.mdx778-915
Literal schemas validate that the input exactly matches a specific constant value:
These schemas correspond to TypeScript's literal types and provide strict equality validation using Object.is() semantics.
Sources: packages/docs/content/api.mdx112-130 packages/zod/src/v4/classic/tests/literal.test.ts1-23
To validate against multiple literal values, pass an array:
Multi-value literals are functionally equivalent to unions but with a more concise syntax. They're particularly useful for record key validation.
Sources: packages/docs/content/api.mdx131-154 play.ts3-8
Literal Schema Flow
The literal schema stores its allowed values in the values property (a PrimitiveSet) and generates a regex pattern for template literal usage. During validation, it performs strict equality checking against each allowed value.
Sources: packages/zod/src/v4/core/schemas.ts2689-2827
The $ZodLiteral constructor handles both single and multi-value literals:
The values property is automatically populated during initialization and used by:
Sources: packages/zod/src/v4/core/schemas.ts123-129 packages/zod/src/v4/core/schemas.ts2689-2827
The z.enum() function creates a schema that validates against a fixed set of string values:
Important: Always pass the array directly or use as const to preserve literal types:
Sources: packages/docs/content/api.mdx780-809
Zod 4 supports TypeScript enums and enum-like objects directly:
This replaces Zod 3's z.nativeEnum() API. TypeScript's enum keyword is generally not recommended, but Zod supports it for compatibility.
Sources: packages/docs/content/api.mdx811-854
Access the enum values as an object using the .enum property:
Sources: packages/docs/content/api.mdx855-877
Create a new enum schema excluding specific values:
This method is only available in the Classic API.
Sources: packages/docs/content/api.mdx878-896
Create a new enum schema containing only specific values:
This method is only available in the Classic API.
Sources: packages/docs/content/api.mdx897-915
Enum Schema Construction and Validation
The enum schema processes its input into an entries object and a values set during initialization. The values set is used for fast validation lookups, while the entries object provides the ergonomic .enum accessor.
Sources: packages/zod/src/v4/core/schemas.ts3219-3319
During construction, the enum schema:
entries objectvalues setSources: packages/zod/src/v4/core/schemas.ts3219-3319
Template literal schemas validate strings that match TypeScript template literal patterns:
The z.templateLiteral() function accepts an array of:
"hello")string | number | bigint | boolean | null | undefinedSources: packages/docs/content/api.mdx606-635
Sources: packages/docs/content/api.mdx617-635
Template literals can be nested and composed with optional schemas:
Sources: packages/zod/src/v4/classic/tests/template-literal.test.ts60-66
Template Literal Pattern Generation
Each part of the template literal contributes a regex pattern segment. String literals are escaped, schemas contribute patterns based on their type and constraints, and everything is concatenated into a final validation pattern.
Sources: packages/zod/src/v4/core/schemas.ts3328-3544
Template literal validation relies on regex patterns extracted from each component:
Pattern extraction rules:
.pattern: Use the schema's pattern(?:...)?|null to the patternOnly schemas that can be represented as regex patterns are allowed in template literals. Complex schemas like objects or arrays will throw an error.
Sources: packages/zod/src/v4/core/schemas.ts3328-3544
Template literal type inference is sophisticated and matches TypeScript's template literal types:
The type system recursively builds up the template literal type from each part, handling optionals, nullables, unions, and more.
Sources: packages/zod/src/v4/classic/tests/template-literal.test.ts100-143
Choosing the Right Schema Type
| Scenario | Recommended Type | Reason |
|---|---|---|
Single constant like "admin" | z.literal() | Simplest, most explicit |
| 2-5 related values | z.literal([...]) | Concise, no extra methods needed |
String enum needing .exclude() | z.enum() | Has utility methods |
| TypeScript enum compatibility | z.enum() | Direct support |
Pattern like id_${string} | z.templateLiteral() | Type-safe string interpolation |
| Complex ID formats | z.templateLiteral() with constraints | Regex-based validation |
Sources: packages/docs/content/api.mdx112-915
All three schema types use the values property internally for fast lookups:
Validation Performance:
O(1) - Direct Object.is() comparisonO(n) - Set lookup, typically very fast for small setsO(1) - Set lookupO(1) - Regex testMemory:
Sources: packages/zod/src/v4/core/schemas.ts123-129 packages/zod/src/v4/core/util.ts1-50
Literal and enum schemas with the values property enable record key validation:
The record schema checks the literal's values property to determine which keys are required vs optional.
Sources: play.ts3-8 packages/zod/src/v4/core/schemas.ts2937-3087
Literal schemas power discriminated union fast paths:
The discriminated union uses the propValues property (derived from values) to route to the correct branch without trying all options.
Sources: packages/zod/src/v4/core/schemas.ts134-135
Schemas with the pattern property can participate in template literals:
The template literal extracts the pattern from the cuid schema to build its validation regex.
Refresh this wiki