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 as itself.

So, additionalProperties can restrict you from "extending" a schema using combining keywords such as allOf. In the following example, we can see how the additionalProperties can cause attempts to extend the address schema example to fail.

1{
2  "allOf": [
3    {
4      "type": "object",
5      "properties": {
6        "street_address": { "type": "string" },
7        "city": { "type": "string" },
8        "state": { "type": "string" }
9      },
10      "required": ["street_address", "city", "state"],
11      "additionalProperties": false
12    }
13  ],
14  "properties": {
15    "type": { "enum": [ "residential", "business" ] }
16  },
17  "required": ["type"]
18}

The above 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.

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.

1{
2  "allOf": [
3    {
4      "type": "object",
5      "properties": {
6        "street_address": { "type": "string" },
7        "city": { "type": "string" },
8        "state": { "type": "string" }
9      },
10      "required": ["street_address", "city", "state"],    }
11  ],
12  "properties": {
13    "type": { "enum": [ "residential", "business" ] }
14  },
15  "unevaluatedProperties": false,
16  "required": ["type"]
17}

Task

You are give the same schema in the side editor. Add unevaluatedProperties to the schema to allow the only number as an additional property.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output