Enum and Const — Fixed Value Validation
Use enum and const in JSON Schema to restrict values to a fixed set or a single literal. Learn mixed-type enums, const for API versioning, and best practices.
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["pending", "processing", "shipped", "delivered", "cancelled"]
},
"priority": {
"enum": [1, 2, 3, "urgent"]
},
"apiVersion": {
"const": "2024-01-01"
},
"currency": {
"type": "string",
"enum": ["USD", "EUR", "GBP", "JPY", "CAD"]
},
"isTest": {
"const": false
}
},
"required": ["status", "apiVersion", "currency"]
}Test Data
{
"status": "shipped",
"priority": "urgent",
"apiVersion": "2024-01-01",
"currency": "USD",
"isTest": false
}Detailed Explanation
Enum and Const — Restricting to Fixed Values
The enum and const keywords restrict a value to a predefined set or a single literal. They are essential for fields with a known list of valid options.
enum — Multiple Allowed Values
{
"enum": ["draft", "published", "archived"]
}
The value must be exactly one of the listed items. Comparison uses deep equality — strings must match exactly (case-sensitive), numbers must be identical, and objects/arrays use structural comparison.
const — Single Allowed Value
{
"const": "2024-01-01"
}
The value must be exactly this one literal. const is equivalent to enum with a single element, but it expresses the intent more clearly and produces better error messages.
How the Example Schema Works
The schema demonstrates several patterns:
status— a string enum with five order statuses. Only these exact strings are accepted —"Shipped"(capitalized) would fail.priority— a mixed-type enum accepting integers 1, 2, 3 or the string"urgent". JSON Schema enums can contain values of different types, unlike enums in most programming languages.apiVersion— aconstfield pinned to"2024-01-01". This ensures requests are explicitly targeting a specific API version. When you release a new version, you update the const and clients must opt in.currency— a string enum with ISO 4217 currency codes. Combiningtype: "string"withenumadds an extra layer of documentation, even though the enum values already imply the type.isTest—const: falseensures this flag is alwaysfalsein production schemas. Test environments use a separate schema withconst: true.
Enum with Descriptions
Standard JSON Schema does not support per-value descriptions in enum. If you need labels or descriptions, use oneOf with const:
{
"oneOf": [
{ "const": "pending", "title": "Pending", "description": "Order received, not yet processed" },
{ "const": "shipped", "title": "Shipped", "description": "Order dispatched to carrier" }
]
}
This pattern is recognized by documentation generators and form builders to display human-readable labels alongside enum values.
Case Sensitivity
JSON string comparison is case-sensitive. The enum ["USD", "EUR"] rejects "usd" and "eur". If you need case-insensitive matching, either normalize the input before validation or list all case variants (which is usually impractical).
Null in Enums
You can include null in an enum to create a nullable enum:
{ "enum": ["active", "inactive", null] }
This is a common pattern for status fields where null represents "not yet determined."
Use Case
Use enum for status fields, category selections, ISO codes (currency, country, language), and any field with a fixed set of valid options. Use const for API version pinning, fixed configuration values, and discriminator fields in oneOf schemas. Together, they prevent invalid data from entering your system at the schema level.