From 74e6c63d4df01ec11f2b2091f27bb4f50e48214d Mon Sep 17 00:00:00 2001 From: gigbeeAtReelo Date: Thu, 13 Nov 2025 12:01:50 +0000 Subject: [PATCH 1/3] Add section for one of in enum --- .../04-Enumerated-Values/instructions.mdx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx index ba3db60..8f2b34e 100644 --- a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx +++ b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx @@ -41,4 +41,19 @@ Now, try to modify the `hobbies` property on the with `enum` c Values defined in `enum` are case-sensitive. So `reading` and `Reading` are considered different values. + + + +There is another nice `keyword` we have which can be used instead of `enum` i.e `oneOf` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#oneOf), this `keyword` validates the given data against exactly one of the given `values/subschemas` + +**Example:** + +```json +{ + "department": { + "oneOf": ["engineering", "marketing", + "sales", "HR", "finance"] + } +} +``` \ No newline at end of file From 5e69d872bca04368b57e8f224581971f8c52389b Mon Sep 17 00:00:00 2001 From: gigbeeAtReelo Date: Thu, 13 Nov 2025 15:48:28 +0000 Subject: [PATCH 2/3] Fix oneof subschemas issue --- .../04-Enumerated-Values/instructions.mdx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx index 8f2b34e..331b3b8 100644 --- a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx +++ b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx @@ -44,15 +44,20 @@ So `reading` and `Reading` are considered different values. -There is another nice `keyword` we have which can be used instead of `enum` i.e `oneOf` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#oneOf), this `keyword` validates the given data against exactly one of the given `values/subschemas` +There is another nice `keyword` we have which can be used instead of `enum` i.e `oneOf` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#oneOf), this `keyword` validates the given data against exactly one of the given `subschemas` and not the values **Example:** ```json { "department": { - "oneOf": ["engineering", "marketing", - "sales", "HR", "finance"] + "oneOf": [ + { type: "string", const: "engineering" }, + { type: "string", const: "marketing" }, + { type: "string", const: "sales" }, + { type: "string", const: "HR" }, + { type: "string", const: "finance" } + ] } } ``` From 561965c6945491fa6c811b5e055139c597f19bb8 Mon Sep 17 00:00:00 2001 From: gigbeeAtReelo Date: Fri, 14 Nov 2025 18:08:34 +0000 Subject: [PATCH 3/3] Fix one of example and add any of explanation --- .../04-Enumerated-Values/instructions.mdx | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx index 331b3b8..b37ec62 100644 --- a/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx +++ b/content/01-Getting-Started/04-Enumerated-Values/instructions.mdx @@ -44,21 +44,45 @@ So `reading` and `Reading` are considered different values. -There is another nice `keyword` we have which can be used instead of `enum` i.e `oneOf` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#oneOf), this `keyword` validates the given data against exactly one of the given `subschemas` and not the values +Instead of using `enum`, you can also use `anyOf` or `oneOf` to validate your data against a set of subschemas. They work similarly, but there's a key difference in how strict they are. + +### anyOf (Recommended) + +Generally `anyof` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#anyOf) is preferred as it validate data against **any** (one or more) of the given subschemas. **Example:** +```json +{ + "department": { + "anyOf": [ + { "const": "engineering" }, + { "const": "marketing" }, + { "const": "sales" }, + { "const": "HR" }, + { "const": "finance" } + ] + } +} +``` -```json +### oneOf + +The `oneOf` [keyword](https://json-schema.org/understanding-json-schema/reference/combining#oneOf) is stricter—it validates only when your data matches exactly one subschema. Since it has to check all possibilities to ensure only one matches, ***it can take longer to process***. + +**Example:** +```json { "department": { "oneOf": [ - { type: "string", const: "engineering" }, - { type: "string", const: "marketing" }, - { type: "string", const: "sales" }, - { type: "string", const: "HR" }, - { type: "string", const: "finance" } + { "const": "engineering" }, + { "const": "marketing" }, + { "const": "sales" }, + { "const": "HR" }, + { "const": "finance" } ] } } ``` + +In this department example, both approaches work identically since each value can only match one const at a time. But stick with `anyOf` as a general practice—it'll save you trouble down the road when your schemas get more complex. \ No newline at end of file