Reference

Enumerated values

The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.

Below are several examples demonstrating its usage.

Basic Example: Street Light Colors

This example demonstrates how to validate that the color property of a street light is either "red", "amber", or "green".

Copy icon
 logo-white schema
{ "properties": { "color": { "enum": ["red", "amber", "green"] } }}
Copy icon
data
{ "color": "red" }
Checkmark iconcompliant to schema
Copy icon
data
{ "color": "blue" }
Error iconnot compliant to schema

Extended Example: Accepting Multiple Data Types

Enums can be used without explicitly setting a data type, allowing different types of values. In the following example, the schema is extended to include null (to represent an "off" state).

Copy icon
 logo-white schema
{ "properties": { "color": { "enum": ["red", "amber", "green", null, 42] } }}
Copy icon
data
{ "color": null }
Checkmark iconcompliant to schema
Copy icon
data
{ "color": 42 }
Checkmark iconcompliant to schema
Copy icon
data
{ "color": 0 }
Error iconnot compliant to schema

Additional Example: Mixed Types for Shape

Copy icon
 logo-white schema
{ "properties": { "shape": { "enum": ["circle", "square", 1, null] } }}
Copy icon
data
{ "shape": "circle" }
Checkmark iconcompliant to schema
Copy icon
data
{ "shape": 1 }
Checkmark iconcompliant to schema
Copy icon
data
{ "shape": null }
Checkmark iconcompliant to schema
Copy icon
data
{ "shape": "triangle" }
Error iconnot compliant to schema

Need Help?

Did you find these docs helpful?

Help us make our docs great!

At JSON Schema, we value docs contributions as much as every other type of contribution!

Still Need Help?

Learning JSON Schema is often confusing, but don't worry, we are here to help!.