additionalProperties : Schema
additionalProperties
SchemaValidation succeeds if the schema validates against each value not matched by other object applicators in this vocabulary.
Value | This keyword must be set to a valid JSON Schema |
---|---|
Kind | Applicator Annotation |
Applies To | Object |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 1 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.2.3 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/additionalProperties.json |
Default |
{}
|
Annotation | Array The set of instance property names validated by this keyword's subschema |
Affected By |
|
Affects |
|
Also See |
|
The additionalProperties
keyword is used to control the handling of properties whose names are not listed in the properties
keyword or match any of the regular expressions in the patternProperties
keyword. By default any additional properties are allowed.
The behavior of this keyword depends on the presence and annotation results of properties
and patternProperties
within the same schema object. Validation with additionalProperties
applies only to the child values of instance names that do not appear in the annotation results of either properties
or patternProperties
.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"foo": { "type": "string" }
},
"additionalProperties": false
}
{ "foo": "foo" }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "foo": "foo", "bar": "bar" }
- When
additionalProperties
is set to false, all the instance properties must either be present in theproperties
or match any regex withinpatternProperties
; otherwise, the validaion will fail.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"name": { "type": "string" }
},
"additionalProperties": {
"type": "number"
}
}
{ "name": "John Doe", "age": 21 }
{ "keyword": "/properties", "instance": "", "value": [ "name" ] },
{ "keyword": "/additionalProperties", "instance": "", "value": [ "age" ] }
{ "name": "John Doe", "age": "21" }
- The value of
additionalProperties
can either be a boolean schema or an object schema.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" }
},
"patternProperties": {
"[Aa]ge$": { "type": "number" }
},
"additionalProperties": true
}
{
"name": [ "John", "Doe" ],
"Age": 21,
"email": "foo@bar.com"
}
{
"name": "John Doe",
"Age": 21,
"email": "foo@bar.com"
}
{ "keyword": "/properties", "instance": "", "value": [ "name" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "Age" ] }
{ "keyword": "/additionalProperties", "instance": "", "value": [ "email" ] }
- Instance properties (keys) not present in
properties
or not matching any regex withinpatternProperties
are evaluated againstadditionalProperties
.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" }
},
"patternProperties": {
"[Aa]ge$": { "type": "number" }
}
}
{
"name": "John Doe",
"Age": 21,
"email": "foo@bar.com"
}
{
"name": "John Doe",
"Age": "21",
"email": "foo@bar.com"
}
{
"name": "John Doe",
"Age": 21,
"email": [ "foo", "@", "bar", "com" ]
}
{ "keyword": "/properties", "instance": "", "value": [ "name" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "Age" ] }
Note: JSON Schema is a constraint language and if you don’t limit keywords like this, then more keywords than what you defined in properties
, etc would be allowed. If you don’t define a property using properties
or patternProperties
, but don’t disallow it with additionalProperties
, it would still be valid with any value.