Skip to content

Commit cf10622

Browse files
committed
Add examples for enum validation in JSON schema reference
1 parent 172c5da commit cf10622

File tree

1 file changed

+47
-9
lines changed
  • pages/understanding-json-schema/reference

1 file changed

+47
-9
lines changed

pages/understanding-json-schema/reference/enum.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ next:
99
url: /understanding-json-schema/reference/const
1010
---
1111

12-
The `enum` [keyword](../../learn/glossary#keyword) is used to restrict a value to a fixed set of values.
13-
It must be an array with at least one element, where each element is
14-
unique.
12+
The `enum` keyword in JSON Schema allows you to restrict a value to a set of predefined options.
13+
It requires an array with at least one unique element, and any value validated against this schema must exactly match one of the specified values.
1514

16-
The following is an example for validating street light colors:
15+
Below are several examples demonstrating its usage.
16+
17+
### Basic Example: Street Light Colors
18+
19+
This example demonstrates how to validate that the `color` property of a street light is either "red", "amber", or "green".
1720

1821
```json
1922
// props { "isSchema": true }
@@ -35,9 +38,10 @@ The following is an example for validating street light colors:
3538
{ "color": "blue" }
3639
```
3740

38-
You can use `enum` even without a type, to accept values of different
39-
types. Let\'s extend the example to use `null` to indicate \"off\", and
40-
also add 42, just for fun.
41+
### Extended Example: Accepting Multiple Data Types
42+
43+
Enums can be used without explicitly setting a data type, allowing different types of values.
44+
In the following example, the schema is extended to include `null` (to represent an "off" state) and the number 42.
4145

4246
```json
4347
// props { "isSchema": true }
@@ -62,5 +66,39 @@ also add 42, just for fun.
6266

6367
```json
6468
// props { "indent": true, "valid": false }
65-
0
66-
```
69+
{ "color": 0 }
70+
```
71+
72+
73+
### Additional Example: Mixed Types for Shape
74+
75+
```json
76+
// props { "isSchema": true }
77+
{
78+
"properties": {
79+
"shape": {
80+
"enum": ["circle", "square", 1, null]
81+
}
82+
}
83+
}
84+
```
85+
86+
```json
87+
// props { "indent": true, "valid": true }
88+
{ "shape": "circle" }
89+
```
90+
91+
```json
92+
// props { "indent": true, "valid": true }
93+
{ "shape": 1 }
94+
```
95+
96+
```json
97+
// props { "indent": true, "valid": true }
98+
{ "shape": null }
99+
```
100+
101+
```json
102+
// props { "indent": true, "valid": false }
103+
{ "shape": "triangle" }
104+
```

0 commit comments

Comments
 (0)