From ca04035f0ff0a444d23d425dae66ee7cf3262a97 Mon Sep 17 00:00:00 2001 From: 29deepanshutyagi <29deepanshutyagi@gmail.com> Date: Mon, 9 Dec 2024 20:20:40 +0530 Subject: [PATCH 1/4] Updated instructions.mdx to use instead of allOf for extending closed schemas --- .../instructions.mdx | 57 +++---------------- 1 file changed, 7 insertions(+), 50 deletions(-) diff --git a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx index e1561ba..75d4d3b 100644 --- a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx +++ b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx @@ -1,64 +1,21 @@ --- title: Extending Closed Schemas with unevaluatedProperties description: Extending Closed Schemas in JSON Schema Objects using the unevaluatedProperties keyword -keywords: extending closed schemas,unevaluatedProperties , JSON Schema, JSON Schema objects +keywords: extending closed schemas, unevaluatedProperties, JSON Schema, JSON Schema objects --- - # Extending Closed Schemas -Previously in the Objects module, we learned to `additionalProperties`. However, it is important to note that `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema) as itself. +Previously in the Objects module, we learned about `additionalProperties`. However, it is important to note that `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema) as itself. -So, `additionalProperties` can restrict you from "extending" a schema using combining [keywords](https://json-schema.org/learn/glossary#subschema) such as `allOf`. In the following example, we can see how the `additionalProperties` can cause attempts to extend the address schema example to fail. +This limitation can restrict you from "extending" a schema using combining [keywords](https://json-schema.org/learn/glossary#subschema) such as `$ref`. In the following example, we use `$ref` to extend the address schema (`https://example.com/address`) by adding a `type` property while controlling additional properties. This approach showcases how `unevaluatedProperties` resolves the challenges of extending schemas. -```json highlightLineStart={11} +```json highlightLineStart={6,13,16} { - "allOf": [ - { - "type": "object", - "properties": { - "street_address": { "type": "string" }, - "city": { "type": "string" }, - "state": { "type": "string" } - }, - "required": ["street_address", "city", "state"], - "additionalProperties": false - } - ], + "$ref": "https://example.com/address", "properties": { "type": { "enum": [ "residential", "business" ] } }, - "required": ["type"] + "required": ["type"], + "unevaluatedProperties": false } -``` -The above [schema](https://json-schema.org/learn/glossary#schema) will not allow you to define `type` property. because `additionalProperties` is set to `false`. The reason is, `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema). - - -## Unevaluated Properties - -The challenge we saw with `additionalProperties` can be solved using the `unevaluatedProperties` keyword. This keyword allows you to define properties that are not evaluated by the current schema. - -```json highlightLineStart={15} -{ - "allOf": [ - { - "type": "object", - "properties": { - "street_address": { "type": "string" }, - "city": { "type": "string" }, - "state": { "type": "string" } - }, - "required": ["street_address", "city", "state"], } - ], - "properties": { - "type": { "enum": [ "residential", "business" ] } - }, - "unevaluatedProperties": false, - "required": ["type"] -} -``` - -## Task - -You are give the same schema in the . Add `unevaluatedProperties` to the schema to allow the only `number` as an additional property. - From 2a826ee8eab6dc43deb488c9f29df21d5950aeb9 Mon Sep 17 00:00:00 2001 From: 29deepanshutyagi <29deepanshutyagi@gmail.com> Date: Mon, 9 Dec 2024 20:49:34 +0530 Subject: [PATCH 2/4] Update instructions.mdx to use unevaluatedProperties for extending closed schemas --- .../instructions.mdx | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx index 75d4d3b..f541e0b 100644 --- a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx +++ b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx @@ -8,14 +8,52 @@ keywords: extending closed schemas, unevaluatedProperties, JSON Schema, JSON Sch Previously in the Objects module, we learned about `additionalProperties`. However, it is important to note that `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema) as itself. -This limitation can restrict you from "extending" a schema using combining [keywords](https://json-schema.org/learn/glossary#subschema) such as `$ref`. In the following example, we use `$ref` to extend the address schema (`https://example.com/address`) by adding a `type` property while controlling additional properties. This approach showcases how `unevaluatedProperties` resolves the challenges of extending schemas. +So, `additionalProperties` can restrict you from "extending" a schema using combining [keywords](https://json-schema.org/learn/glossary#subschema) such as `$ref`. In the following example, we can see how the `additionalProperties` can cause attempts to extend the address schema example to fail. -```json highlightLineStart={6,13,16} +```json highlightLineStart={11} +{ + "type": "object", + "properties": { + "street_address": { "type": "string" }, + "city": { "type": "string" }, + "state": { "type": "string" } + }, + "required": ["street_address", "city", "state"], + "additionalProperties": false +} + +``` + +The above [schema](https://json-schema.org/learn/glossary#schema) will not allow you to define the `type` property because `additionalProperties` is set to `false`. The reason is, `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema). + +## Unevaluated Properties + +The challenge we saw with `additionalProperties` can be solved using the `unevaluatedProperties` keyword. This keyword allows you to define properties that are not evaluated by the current schema. + +```json highlightLineStart={15} { "$ref": "https://example.com/address", "properties": { "type": { "enum": [ "residential", "business" ] } }, + "unevaluatedProperties": { "type": "number" }, "required": ["type"], - "unevaluatedProperties": false + "definitions": { + "address": { + "type": "object", + "properties": { + "street_address": { "type": "string" }, + "city": { "type": "string" }, + "state": { "type": "string" } + }, + "required": ["street_address", "city", "state"] + } + } } +``` + +## Task + +You are given the same schema in the . Add `unevaluatedProperties` to the schema to allow only `number` as an additional property. + +--- From e341d79e39374fca5292f233c6a46e446c670eee Mon Sep 17 00:00:00 2001 From: 29deepanshutyagi <29deepanshutyagi@gmail.com> Date: Mon, 9 Dec 2024 20:52:23 +0530 Subject: [PATCH 3/4] Refactor schema to use unevaluatedProperties for extending closed schemas --- .../instructions.mdx | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx index f541e0b..19b8e10 100644 --- a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx +++ b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx @@ -12,16 +12,26 @@ So, `additionalProperties` can restrict you from "extending" a schema using comb ```json highlightLineStart={11} { - "type": "object", + "$ref": "https://example.com/address", "properties": { - "street_address": { "type": "string" }, - "city": { "type": "string" }, - "state": { "type": "string" } + "type": { "enum": ["residential", "business"] } }, - "required": ["street_address", "city", "state"], - "additionalProperties": false + "required": ["type"], + "additionalProperties": false, + "definitions": { + "address": { + "type": "object", + "properties": { + "street_address": { "type": "string" }, + "city": { "type": "string" }, + "state": { "type": "string" } + }, + "required": ["street_address", "city", "state"] + } + } } + ``` The above [schema](https://json-schema.org/learn/glossary#schema) will not allow you to define the `type` property because `additionalProperties` is set to `false`. The reason is, `additionalProperties` only recognizes properties declared in the same [subschema](https://json-schema.org/learn/glossary#subschema). From c00b9a05f9ef729b20bb623b274d27623d9ed909 Mon Sep 17 00:00:00 2001 From: 29deepanshutyagi <29deepanshutyagi@gmail.com> Date: Mon, 9 Dec 2024 21:05:34 +0530 Subject: [PATCH 4/4] Refactor schema to use unevaluatedProperties for extending closed schemas --- .../code.ts | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/code.ts b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/code.ts index af3edf7..c2de053 100644 --- a/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/code.ts +++ b/content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/code.ts @@ -1,22 +1,18 @@ const code: any = { - allOf: [ - { - type: "object", - properties: { - street_address: { type: "string" }, - city: { type: "string" }, - state: { type: "string" }, - }, - required: ["street_address", "city", "state"], - }, - ], + $ref: "https://example.com/address", properties: { + street_address: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, type: { enum: ["residential", "business"] }, }, - required: ["type"], + required: ["street_address", "city", "state", "type"], + additionalProperties: false }; let solution = structuredClone(code); + + solution.unevaluatedProperties = { type: "number", }; @@ -39,7 +35,7 @@ const testCases = [ type: "business", zip: 20500, }, - expected: true, + expected: true, }, { input: { @@ -47,9 +43,9 @@ const testCases = [ city: "Washington", state: "DC", type: "business", - zip: "20500", + zip: "20500", }, - expected: false, + expected: false, }, { input: { @@ -57,7 +53,7 @@ const testCases = [ city: "Washington", state: "DC", type: "business", - zip: null, + zip: null, }, expected: false, },