|
9 | 9 | url: /understanding-json-schema/reference/const
|
10 | 10 | ---
|
11 | 11 |
|
| 12 | + |
12 | 13 | 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 | +``` |
15 | 41 |
|
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. |
17 | 46 |
|
18 | 47 | ```json
|
19 | 48 | // props { "isSchema": true }
|
20 | 49 | {
|
21 |
| - "enum": ["red", "amber", "green"] |
| 50 | + "properties": { |
| 51 | + "color": { |
| 52 | + "enum": ["red", "amber", "green", null, 42] |
| 53 | + } |
| 54 | + } |
22 | 55 | }
|
23 | 56 | ```
|
| 57 | + |
24 | 58 | ```json
|
25 | 59 | // props { "indent": true, "valid": true }
|
26 |
| -"red" |
| 60 | +{ "color": null } |
27 | 61 | ```
|
| 62 | + |
| 63 | +```json |
| 64 | +// props { "indent": true, "valid": true } |
| 65 | +{ "color": 42 } |
| 66 | +``` |
| 67 | + |
28 | 68 | ```json
|
29 | 69 | // props { "indent": true, "valid": false }
|
30 |
| -"blue" |
| 70 | +{ "color": 0 } |
31 | 71 | ```
|
32 | 72 |
|
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 |
36 | 75 |
|
37 | 76 | ```json
|
38 | 77 | // props { "isSchema": true }
|
39 | 78 | {
|
40 |
| - "enum": ["red", "amber", "green", null, 42] |
| 79 | + "properties": { |
| 80 | + "shape": { |
| 81 | + "enum": ["circle", "square", 1, null] |
| 82 | + } |
| 83 | + } |
41 | 84 | }
|
42 | 85 | ```
|
| 86 | + |
43 | 87 | ```json
|
44 | 88 | // props { "indent": true, "valid": true }
|
45 |
| -"red" |
| 89 | +{ "shape": "circle" } |
46 | 90 | ```
|
| 91 | + |
47 | 92 | ```json
|
48 | 93 | // props { "indent": true, "valid": true }
|
49 |
| -null |
| 94 | +{ "shape": 1 } |
50 | 95 | ```
|
| 96 | + |
51 | 97 | ```json
|
52 | 98 | // props { "indent": true, "valid": true }
|
53 |
| -42 |
| 99 | +{ "shape": null } |
54 | 100 | ```
|
| 101 | + |
55 | 102 | ```json
|
56 | 103 | // props { "indent": true, "valid": false }
|
57 |
| -0 |
58 |
| -``` |
| 104 | +{ "shape": "triangle" } |
| 105 | +``` |
0 commit comments