enum to restrict a field to a list of allowed values.
Basic usage
Mixed-type enums
enum values are not restricted to strings:
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Restrict a value to a fixed set of choices.
enum to restrict a field to a list of allowed values.
{
"type": "object",
"properties": {
"priority": { "enum": ["low", "medium", "high"] },
"status": { "enum": ["open", "in_progress", "closed"] }
},
"required": ["priority", "status"],
"additionalProperties": false
}
from typing import Literal
from pydantic import BaseModel, ConfigDict
class Payload(BaseModel):
model_config = ConfigDict(extra="forbid")
priority: Literal["low", "medium", "high"]
status: Literal["open", "in_progress", "closed"]
import { z } from "zod";
const payloadSchema = z
.object({
priority: z.enum(["low", "medium", "high"]),
status: z.enum(["open", "in_progress", "closed"]),
})
.strict();
enum values are not restricted to strings:
{
"type": "object",
"properties": {
"score": { "enum": [1, 2, 3, "unknown", null] }
},
"required": ["score"],
"additionalProperties": false
}
from typing import Literal
from pydantic import BaseModel, ConfigDict
class Payload(BaseModel):
model_config = ConfigDict(extra="forbid")
score: Literal[1, 2, 3, "unknown"] | None
import { z } from "zod";
const payloadSchema = z
.object({
score: z.union([
z.literal(1),
z.literal(2),
z.literal(3),
z.literal("unknown"),
z.null(),
]),
})
.strict();