Skip to content

Latest commit

 

History

History
236 lines (211 loc) · 6.58 KB

additionalProperties.markdown

File metadata and controls

236 lines (211 loc) · 6.58 KB
keyword signature summary kind instance specification metaschema index introduced_in interdependencies related
additionalProperties
Schema
Validation succeeds if the schema validates against each value not matched by other object applicators in this vocabulary.
applicator
annotation
object
-998
draft1
vocabulary keyword
applicator
properties
vocabulary keyword
applicator
patternProperties
vocabulary keyword
applicator
dependentSchemas
vocabulary keyword
applicator
propertyNames
vocabulary keyword
validation
required
vocabulary keyword
validation
dependentRequired
vocabulary keyword
validation
minProperties
vocabulary keyword
validation
maxProperties
vocabulary keyword
unevaluated
unevaluatedProperties

Annotations

The annotation result of this keyword is the set of instance property names validated by this keyword.

Explanation

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.

  • The value of additionalProperties must be a valid JSON Schema.
  • Each property value of this object must be a valid JSON Schema.
  • The annotation result of this keyword is the set of instance property names validated by this keyword's subschema.
  • This annotation affects the behavior of unevaluatedProperties in the Unevaluated vocabulary.
  • Omitting this keyword has the same assertion behavior as an empty object.

Examples

{{<schema Schema with 'additionalProperties' set to boolean false>}} { "$schema": "https://json-schema.org/draft/2020-12/schema", "properties": { "foo": { "type": "string" } }, "additionalProperties": false } {{}}

{{<instance-pass An instance with no additional properties is valid>}} { "foo": "foo" } {{}}

{{}} [ // ... { "valid": true, "keywordLocation": "/properties", "instanceLocation": "", "annotation": [ "foo" ] }, // ... ] {{}}

{{<instance-fail An instance with additional properties is invalid>}} { "foo": "foo", "bar": "bar" } {{}}

  • When additionalProperties is set to false, all the instance properties must either be present in the properties or match any regex within patternProperties; otherwise, the validaion will fail.

{{<schema Schema with 'additionalProperties' set to an object schema>}} { "$schema": "https://json-schema.org/draft/2020-12/schema", "properties": { "name": { "type": "string" } }, "additionalProperties": { "type": "number" } } {{}}

{{<instance-pass An object instance with properties conforming to the schema is valid>}} { "name": "John Doe", "age": 21 } {{}}

{{}} [ // ... { "valid": true, "keywordLocation": "/properties", "instanceLocation": "", "annotation": [ "name" ] }, { "valid": true, "keywordLocation": "/additionalProperties", "instanceLocation": "", "annotation": [ "age" ] }, // ... ] {{}}

{{<instance-fail The value of 'age' must be a number>}} { "name": "John Doe", "age": "21" } {{}}

  • The value of additionalProperties can either be a boolean schema or an object schema.

{{<schema Schema with 'patternProperties', 'properties' and 'additionalProperties' keyword>}} { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "name": { "type": "string" } }, "patternProperties": { "[Aa]ge$": { "type": "number" } }, "additionalProperties": true } {{}}

{{<instance-fail The value of the 'name' property must be a string>}} { "name": [ "John", "Doe" ], "Age": 21, "email": "foo@bar.com" } {{}}

{{<instance-pass An object instance with properties conforming to the schema is valid>}} { "name": "John Doe", "Age": 21, "email": "foo@bar.com" } {{}}

{{}} [ // ... { "valid": true, "keywordLocation": "/properties", "instanceLocation": "", "annotation": [ "name" ] }, { "valid": true, "keywordLocation": "/patternProperties", "instanceLocation": "", "annotation": [ "Age" ] }, { "valid": true, "keywordLocation": "/additionalProperties", "instanceLocation": "", "annotation": [ "email" ] }, // ... ] {{}}

  • Instance properties (keys) not present in properties or not matching any regex within patternProperties are evaluated against additionalProperties.

{{<schema Schema with no 'additionalProperties' defined>}} { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "name": { "type": "string" } }, "patternProperties": { "[Aa]ge$": { "type": "number" } } } {{}}

{{<instance-pass An object instance with properties conforming to the schema is valid>}} { "name": "John Doe", "Age": 21, "email": "foo@bar.com" } {{}}

{{<instance-fail The value of 'Age' must be a number>}} { "name": "John Doe", "Age": "21", "email": "foo@bar.com" } {{}}

{{<instance-pass An object instance with additional properties is valid>}} { "name": "John Doe", "Age": 21, "email": [ "foo", "@", "bar", "com" ] } {{}}

{{}} [ // ... { "valid": true, "keywordLocation": "/properties", "instanceLocation": "", "annotation": [ "name" ] }, { "valid": true, "keywordLocation": "/patternProperties", "instanceLocation": "", "annotation": [ "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.