From 492558e02072923617110b69913a01536e7ac7b6 Mon Sep 17 00:00:00 2001 From: techmannih Date: Tue, 14 Jan 2025 18:09:34 +0530 Subject: [PATCH 1/5] improve enum doc --- .../reference/enum.md | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/pages/understanding-json-schema/reference/enum.md b/pages/understanding-json-schema/reference/enum.md index 2883bbad7..441e7b264 100644 --- a/pages/understanding-json-schema/reference/enum.md +++ b/pages/understanding-json-schema/reference/enum.md @@ -1,12 +1,6 @@ --- title: "Enumerated values" section: docs -prev: - label: Comments - url: /understanding-json-schema/reference/comments -next: - label: Constant values - url: /understanding-json-schema/reference/const --- The `enum` [keyword](../../learn/glossary#keyword) is used to restrict a value to a fixed set of values. @@ -18,16 +12,21 @@ The following is an example for validating street light colors: ```json // props { "isSchema": true } { - "enum": ["red", "amber", "green"] + "properties": { + "color": { + "enum": ["red", "amber", "green"] + } + } } ``` ```json // props { "indent": true, "valid": true } -"red" +{ "color": "red" } ``` + ```json // props { "indent": true, "valid": false } -"blue" +{ "color": "blue" } ``` You can use `enum` even without a type, to accept values of different @@ -37,22 +36,25 @@ also add 42, just for fun. ```json // props { "isSchema": true } { - "enum": ["red", "amber", "green", null, 42] + "properties": { + "color": { + "enum": ["red", "amber", "green", null, 42] + } + } } ``` + ```json // props { "indent": true, "valid": true } -"red" -``` -```json -// props { "indent": true, "valid": true } -null +{ "color": null } ``` + ```json // props { "indent": true, "valid": true } -42 +{ "color": 42 } ``` + ```json // props { "indent": true, "valid": false } -0 -``` +{ "color": "blue" } +``` \ No newline at end of file From 382fa99d87446d3004e7fe5274c64eba9d280531 Mon Sep 17 00:00:00 2001 From: techmannih Date: Tue, 14 Jan 2025 18:16:42 +0530 Subject: [PATCH 2/5] add prev and next --- pages/understanding-json-schema/reference/enum.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pages/understanding-json-schema/reference/enum.md b/pages/understanding-json-schema/reference/enum.md index 441e7b264..8a1914fbe 100644 --- a/pages/understanding-json-schema/reference/enum.md +++ b/pages/understanding-json-schema/reference/enum.md @@ -1,6 +1,12 @@ --- title: "Enumerated values" section: docs +prev: + label: Comments + url: /understanding-json-schema/reference/comments +next: + label: Constant values + url: /understanding-json-schema/reference/const --- The `enum` [keyword](../../learn/glossary#keyword) is used to restrict a value to a fixed set of values. From cf106229059c271ec0852df231ff70483d771667 Mon Sep 17 00:00:00 2001 From: techmannih Date: Tue, 4 Mar 2025 23:23:29 +0530 Subject: [PATCH 3/5] Add examples for enum validation in JSON schema reference --- .../reference/enum.md | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/pages/understanding-json-schema/reference/enum.md b/pages/understanding-json-schema/reference/enum.md index 260d81261..ee73ccbb6 100644 --- a/pages/understanding-json-schema/reference/enum.md +++ b/pages/understanding-json-schema/reference/enum.md @@ -9,11 +9,14 @@ 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. +The `enum` keyword in JSON Schema allows you to restrict a value to a set of predefined options. +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. -The following is an example for validating street light colors: +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 } @@ -35,9 +38,10 @@ The following is an example for validating street light colors: { "color": "blue" } ``` -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. +### 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 } @@ -62,5 +66,39 @@ also add 42, just for fun. ```json // props { "indent": true, "valid": false } -0 -``` \ No newline at end of file +{ "color": 0 } +``` + + +### Additional Example: Mixed Types for Shape + +```json +// props { "isSchema": true } +{ + "properties": { + "shape": { + "enum": ["circle", "square", 1, null] + } + } +} +``` + +```json +// props { "indent": true, "valid": true } +{ "shape": "circle" } +``` + +```json +// props { "indent": true, "valid": true } +{ "shape": 1 } +``` + +```json +// props { "indent": true, "valid": true } +{ "shape": null } +``` + +```json +// props { "indent": true, "valid": false } +{ "shape": "triangle" } +``` From a478bdb00506c6648764fe43d712807d605ed9ad Mon Sep 17 00:00:00 2001 From: techmannih Date: Thu, 6 Mar 2025 14:52:33 +0530 Subject: [PATCH 4/5] improve doc --- pages/understanding-json-schema/reference/enum.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pages/understanding-json-schema/reference/enum.md b/pages/understanding-json-schema/reference/enum.md index ee73ccbb6..dde85c27a 100644 --- a/pages/understanding-json-schema/reference/enum.md +++ b/pages/understanding-json-schema/reference/enum.md @@ -9,8 +9,9 @@ next: url: /understanding-json-schema/reference/const --- -The `enum` keyword in JSON Schema allows you to restrict a value to a set of predefined options. -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. + +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. Below are several examples demonstrating its usage. @@ -41,7 +42,7 @@ This example demonstrates how to validate that the `color` property of a street ### 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. +In the following example, the schema is extended to include `null` (to represent an "off" state). ```json // props { "isSchema": true } From cc0acf7bdb9015e29d0d7c6dd5054f1a0fea0327 Mon Sep 17 00:00:00 2001 From: techmannih Date: Thu, 13 Mar 2025 09:04:34 +0530 Subject: [PATCH 5/5] add no --- pages/understanding-json-schema/reference/enum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/understanding-json-schema/reference/enum.md b/pages/understanding-json-schema/reference/enum.md index dde85c27a..bb660e4ea 100644 --- a/pages/understanding-json-schema/reference/enum.md +++ b/pages/understanding-json-schema/reference/enum.md @@ -42,7 +42,7 @@ This example demonstrates how to validate that the `color` property of a street ### 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). +In the following example, the schema is extended to include `null` (to represent an "off" state) and the number 42. ```json // props { "isSchema": true }