Skip to content

Commit 2012305

Browse files
authored
improve Enum documentation (#1312)
* improve enum doc * add prev and next * Add examples for enum validation in JSON schema reference * improve doc * add no
1 parent 5458a38 commit 2012305

File tree

1 file changed

+62
-15
lines changed
  • pages/understanding-json-schema/reference

1 file changed

+62
-15
lines changed

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

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,97 @@ next:
99
url: /understanding-json-schema/reference/const
1010
---
1111

12+
1213
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.
14+
It must be an array with at least one element, where each element is unique.
15+
16+
Below are several examples demonstrating its usage.
17+
18+
### Basic Example: Street Light Colors
19+
20+
This example demonstrates how to validate that the `color` property of a street light is either "red", "amber", or "green".
21+
22+
```json
23+
// props { "isSchema": true }
24+
{
25+
"properties": {
26+
"color": {
27+
"enum": ["red", "amber", "green"]
28+
}
29+
}
30+
}
31+
```
32+
```json
33+
// props { "indent": true, "valid": true }
34+
{ "color": "red" }
35+
```
36+
37+
```json
38+
// props { "indent": true, "valid": false }
39+
{ "color": "blue" }
40+
```
1541

16-
The following is an example for validating street light colors:
42+
### Extended Example: Accepting Multiple Data Types
43+
44+
Enums can be used without explicitly setting a data type, allowing different types of values.
45+
In the following example, the schema is extended to include `null` (to represent an "off" state) and the number 42.
1746

1847
```json
1948
// props { "isSchema": true }
2049
{
21-
"enum": ["red", "amber", "green"]
50+
"properties": {
51+
"color": {
52+
"enum": ["red", "amber", "green", null, 42]
53+
}
54+
}
2255
}
2356
```
57+
2458
```json
2559
// props { "indent": true, "valid": true }
26-
"red"
60+
{ "color": null }
2761
```
62+
63+
```json
64+
// props { "indent": true, "valid": true }
65+
{ "color": 42 }
66+
```
67+
2868
```json
2969
// props { "indent": true, "valid": false }
30-
"blue"
70+
{ "color": 0 }
3171
```
3272

33-
You can use `enum` even without a type, to accept values of different
34-
types. Let\'s extend the example to use `null` to indicate \"off\", and
35-
also add 42, just for fun.
73+
74+
### Additional Example: Mixed Types for Shape
3675

3776
```json
3877
// props { "isSchema": true }
3978
{
40-
"enum": ["red", "amber", "green", null, 42]
79+
"properties": {
80+
"shape": {
81+
"enum": ["circle", "square", 1, null]
82+
}
83+
}
4184
}
4285
```
86+
4387
```json
4488
// props { "indent": true, "valid": true }
45-
"red"
89+
{ "shape": "circle" }
4690
```
91+
4792
```json
4893
// props { "indent": true, "valid": true }
49-
null
94+
{ "shape": 1 }
5095
```
96+
5197
```json
5298
// props { "indent": true, "valid": true }
53-
42
99+
{ "shape": null }
54100
```
101+
55102
```json
56103
// props { "indent": true, "valid": false }
57-
0
58-
```
104+
{ "shape": "triangle" }
105+
```

0 commit comments

Comments
 (0)