Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $ref instead of allOf for Extending Closed Schemas #65 #117

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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",
};
Expand All @@ -39,25 +35,25 @@ const testCases = [
type: "business",
zip: 20500,
},
expected: true,
expected: true,
},
{
input: {
street_address: "1600 Pennsylvania Avenue NW",
city: "Washington",
state: "DC",
type: "business",
zip: "20500",
zip: "20500",
},
expected: false,
expected: false,
},
{
input: {
street_address: "1600 Pennsylvania Avenue NW",
city: "Washington",
state: "DC",
type: "business",
zip: null,
zip: null,
},
expected: false,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <SideEditorLink/>. Add `unevaluatedProperties` to the schema to allow the only `number` as an additional property.
You are given the same schema in the <SideEditorLink/>. Add `unevaluatedProperties` to the schema to allow only `number` as an additional property.

---