Skip to content

improve Enum documentation #1312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions pages/understanding-json-schema/reference/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,97 @@ next:
url: /understanding-json-schema/reference/const
---


The `enum` [keyword](../../learn/glossary#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.
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".

```json
// props { "isSchema": true }
{
"properties": {
"color": {
"enum": ["red", "amber", "green"]
}
}
}
```
```json
// props { "indent": true, "valid": true }
{ "color": "red" }
```

```json
// props { "indent": true, "valid": false }
{ "color": "blue" }
```

The following is an example for validating street light colors:
### 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) and the number 42.

```json
// props { "isSchema": true }
{
"enum": ["red", "amber", "green"]
"properties": {
"color": {
"enum": ["red", "amber", "green", null, 42]
}
}
}
```

```json
// props { "indent": true, "valid": true }
"red"
{ "color": null }
```

```json
// props { "indent": true, "valid": true }
{ "color": 42 }
```

```json
// props { "indent": true, "valid": false }
"blue"
{ "color": 0 }
```

You can use `enum` even without a type, to accept values of different
types. Let\'s extend the example to use `null` to indicate \"off\", and
also add 42, just for fun.

### Additional Example: Mixed Types for Shape

```json
// props { "isSchema": true }
{
"enum": ["red", "amber", "green", null, 42]
"properties": {
"shape": {
"enum": ["circle", "square", 1, null]
}
}
}
```

```json
// props { "indent": true, "valid": true }
"red"
{ "shape": "circle" }
```

```json
// props { "indent": true, "valid": true }
null
{ "shape": 1 }
```

```json
// props { "indent": true, "valid": true }
42
{ "shape": null }
```

```json
// props { "indent": true, "valid": false }
0
```
{ "shape": "triangle" }
```