Skip to content

Commit fd0346d

Browse files
committed
Add ref instead all of for additionalproperty example
1 parent 78df994 commit fd0346d

2 files changed

Lines changed: 40 additions & 33 deletions

File tree

content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/code.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const code: any = {
2-
allOf: [
3-
{
2+
$ref: "#/$defs/address",
3+
properties: {
4+
type: { enum: ["residential", "business"] },
5+
},
6+
unevaluatedProperties: false,
7+
required: ["type"],
8+
$defs: {
9+
address: {
410
type: "object",
511
properties: {
612
street_address: { type: "string" },
@@ -9,11 +15,7 @@ const code: any = {
915
},
1016
required: ["street_address", "city", "state"],
1117
},
12-
],
13-
properties: {
14-
type: { enum: ["residential", "business"] },
1518
},
16-
required: ["type"],
1719
};
1820

1921
let solution = structuredClone(code);

content/07-Miscellaneous/01-Extending-Closed-Schemas-with-unevaluatedProperties/instructions.mdx

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
11
---
22
title: Extending Closed Schemas with unevaluatedProperties
3-
description: "Learn how to extend closed schemas in JSON Schema objects using the unevaluatedProperties keyword to allow additional properties, overcoming the limitations of additionalProperties in subschemas."
4-
keywords: "extending closed schemas, unevaluatedProperties, JSON Schema, JSON Schema objects, additionalProperties, subschemas, allOf, combining keywords"
3+
description: "Learn how to extend closed schemas in JSON Schema using the unevaluatedProperties keyword with $ref, enabling safe schema composition while maintaining type safety."
4+
keywords: "extending closed schemas, unevaluatedProperties, JSON Schema, $ref, $defs, additionalProperties, subschemas, schema composition"
55
---
66

7-
87
# Extending Closed Schemas
98

10-
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.
9+
Previously in the Objects , 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.
10+
11+
So, `additionalProperties` can restrict you from "extending" a schema using combining keywords such as `$ref`. In the following example, we can see how the `additionalProperties` can cause attempts to extend the address schema example to fail.
1112

12-
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.
13+
## The Problem with `additionalProperties`
1314

14-
```json highlightLineStart={11}
15+
Let's say we have a reusable address schema in `$defs` that we want to extend. If we try to use `additionalProperties` to keep it closed, we run into issues:
16+
```json highlightLineStart={6}
1517
{
16-
"allOf": [
17-
{
18+
"$ref": "#/$defs/address",
19+
"properties": {
20+
"type": { "enum": ["residential", "business"] }
21+
},
22+
"additionalProperties": false,
23+
"required": ["type"],
24+
"$defs": {
25+
"address": {
1826
"type": "object",
1927
"properties": {
2028
"street_address": { "type": "string" },
2129
"city": { "type": "string" },
2230
"state": { "type": "string" }
2331
},
24-
"required": ["street_address", "city", "state"],
25-
"additionalProperties": false
32+
"required": ["street_address", "city", "state"]
2633
}
27-
],
28-
"properties": {
29-
"type": { "enum": [ "residential", "business" ] }
30-
},
31-
"required": ["type"]
34+
}
3235
}
3336
```
34-
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).
3537

38+
This [schema](https://json-schema.org/learn/glossary#schema) will **reject valid data** because `additionalProperties: false` only sees the `type` property defined locally. It doesn't recognize the properties from the referenced schema (`street_address`, `city`, `state`), so it would incorrectly treat them as "additional" properties and reject them.
3639

3740
## Unevaluated Properties
3841

39-
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.
42+
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.
4043

41-
```json highlightLineStart={15}
44+
```json highlightLineStart={6}
4245
{
43-
"allOf": [
44-
{
46+
"$ref": "#/$defs/address",
47+
"properties": {
48+
"type": { "enum": ["residential", "business"] }
49+
},
50+
"unevaluatedProperties": false,
51+
"required": ["type"],
52+
"$defs": {
53+
"address": {
4554
"type": "object",
4655
"properties": {
4756
"street_address": { "type": "string" },
4857
"city": { "type": "string" },
4958
"state": { "type": "string" }
5059
},
51-
"required": ["street_address", "city", "state"], }
52-
],
53-
"properties": {
54-
"type": { "enum": [ "residential", "business" ] }
55-
},
56-
"unevaluatedProperties": false,
57-
"required": ["type"]
60+
"required": ["street_address", "city", "state"]
61+
}
62+
}
5863
}
5964
```
6065

0 commit comments

Comments
 (0)