-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: main
Are you sure you want to change the base?
Changes from all commits
ca04035
2a826ee
e341d79
c00b9a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
|
||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary and too much whitespace. |
||
solution.unevaluatedProperties = { | ||
type: "number", | ||
}; | ||
|
@@ -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, | ||
}, | ||
|
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```json highlightLineStart={11} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't update the line highlight when you changed the schema.
Suggested change
|
||||||
{ | ||||||
"allOf": [ | ||||||
{ | ||||||
"$ref": "https://example.com/address", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't make sense. What are you expecting this to reference? This can be made to make sense if you reference the address definition.
Suggested change
|
||||||
"properties": { | ||||||
"type": { "enum": ["residential", "business"] } | ||||||
}, | ||||||
"required": ["type"], | ||||||
"additionalProperties": false, | ||||||
"definitions": { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tour uses 2020-12.
Suggested change
|
||||||
"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"] | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this whitespace. |
||||||
``` | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence doesn't make sense. It's certainly not a description of
Suggested change
This version might be too technical to make sense to someone trying to learn JSON Schema, but at least its correct. If someone can make it more accessible, please help. |
||||||
|
||||||
```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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's not actually the task that's expected. |
||||||
|
||||||
--- | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this for? Seems unnecessary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what you were trying to do with this, but it doesn't make sense. It should be something like the following.