Additional Properties
In the previous lesson, we have set additionalProperties to false. What does it mean?
So far, we have seen how to define the properties of an object in a schema. But what if you want to specify constrains for the properties which you have not defined in the schema?
The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword.
Example I
By default any additional properties are allowed. you can set additionalProperties to false to disallow any additional properties.
1{ 2 "type": "object", 3 "properties": { 4 "name": {...}, 5 "age": {...} 6 }, 7 "additionalProperties": false 8}
The above schema will allow only name and age properties in the object. If any other property is present in the object, it will be considered invalid.
Example II
You can also set additionalProperties to a schema to define the schema for the additional properties.
1{ 2 "type": "object", 3 "properties": { 4 "name": {...}, 5 "age": {...} 6 }, 7 "additionalProperties": { 8 "type": "string" 9 } 10}
The above schema will allow only name and age properties in the object. If any other property is present in the object, it should be of type string.
Now, try to modify the schema provided in the side editor on the right to allow any additional properties of type integer.