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, }, 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..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 @@ -1,64 +1,69 @@ --- 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. +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={11} { - "allOf": [ - { + "$ref": "https://example.com/address", + "properties": { + "type": { "enum": ["residential", "business"] } + }, + "required": ["type"], + "additionalProperties": false, + "definitions": { + "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, - "required": ["street_address", "city", "state"], - "additionalProperties": false + "required": ["street_address", "city", "state"] } - ], - "properties": { - "type": { "enum": [ "residential", "business" ] } - }, - "required": ["type"] + } } + + ``` -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). +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. +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": [ - { + "$ref": "https://example.com/address", + "properties": { + "type": { "enum": [ "residential", "business" ] } + }, + "unevaluatedProperties": { "type": "number" }, + "required": ["type"], + "definitions": { + "address": { "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"] + "required": ["street_address", "city", "state"] + } + } } ``` -## Task +## Task -You are give the same schema in the . Add `unevaluatedProperties` to the schema to allow the only `number` as an additional property. +You are given the same schema in the . Add `unevaluatedProperties` to the schema to allow only `number` as an additional property. +---