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 |
Base 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 restricts object instance properties not
described by the properties
and patternProperties
keywords (if any), to validate against the given subschema. Information about
the properties that this keyword was evaluated for is reported using
annotations.
Common Pitfall
The use of the properties
keyword does not prevent the presence of
other properties in the object instance and does not enforce the presence
of the declared properties. In other words, additional data that is not
explicitly prohibited is permitted by default. This is intended behaviour to
ease schema evolution (open schemas are backwards compatible by default) and to
enable highly-expressive constraint-driven schemas.
If you want to restrict instances to only contain the properties you declared,
you must set this keyword to the boolean schema false
, and if you want to
enforce the presence of certain properties, you must use the required
keyword accordingly.
Digging Deeper
While the most common use of this keyword is setting it to the boolean schema
false
to prevent additional properties, it is possible to
set it to a satisfiable schema. Doing this, while omitting the
properties
and
patternProperties
keywords, is an elegant way of describing how the value of every property in
the object instance must look like independently of its
name.
Remember that JSON Schema is a constraint-driven language.
Therefore, non-object instances successfully validate against this
keyword. If needed, make use of the type
keyword to constraint
the accepted type accordingly.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"foo": { "type": "string" }
},
"patternProperties": {
"^x-": { "type": "integer" }
},
"additionalProperties": false
}
{ "foo": "bar", "x-test": 2 }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "x-test" ] }
{ "foo": "bar", "x-test": 2, "extra": true }
{ "extra": true, "random": 1234 }
{}
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": { "type": "integer" }
}
{ "foo": 1, "bar": 2, "baz": 3 }
{ "keyword": "/additionalProperties", "instance": "", "value": [ "foo", "bar", "baz" ] }
{ "foo": 1, "name": "John Doe" }
{}
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"foo": { "type": "string" }
},
"patternProperties": {
"^x-": { "type": "integer" }
},
"additionalProperties": {
"type": "boolean"
}
}
{ "foo": "bar", "x-test": 2, "extra": true }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "x-test" ] }
{ "keyword": "/additionalProperties", "instance": "", "value": [ "extra" ] }
{ "foo": "bar", "x-test": 2, "extra": "should be a boolean" }
{}
"Hello World"