properties : Object<String, Schema>
properties
Object<String, Schema>Validation succeeds if, for each name that appears in both the instance and as a name within this keyword’s value, the child instance for that name successfully validates against the corresponding schema.
Value | This keyword must be set to an object where each value is 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.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/properties.json |
Default |
{}
|
Annotation | Array The set of instance property names validated by this keyword's subschema |
Affected By | None |
Affects |
|
Also See |
|
The properties
keyword restricts properties of an object instance, when
present, to match their corresponding subschemas definitions. Information about
the properties that this keyword was evaluated for is reported using
annotations.
Common Pitfall
The use of this 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 the additionalProperties
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
Setting properties defined by this keyword to the boolean schema
false
is an common trick to express that such properties are
forbidden. This is considered more elegant (and usually more performant) than
using the not
applicator to negate
the required
keyword. However, setting properties defined by this keyword to the
boolean true
is considered to be redundant and an anti-pattern, as additional
properties are permitted by default.
Common Pitfall
This keyword is evaluated independently of the
patternProperties
keyword. If an object property is described by both keywords, then both schemas
must successfully validate against the given property for validation to
succeed.
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": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
}
{ "name": "John Doe", "age": 50 }
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
{ "name": "John Doe" }
{ "keyword": "/properties", "instance": "", "value": [ "name" ] }
{}
{ "name": "John Doe", "age": "this should have been an integer" }
{ "name": 999 }
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"forbidden": false,
"permitted": true
}
}
{ "permitted": "anything is valid" }
{ "keyword": "/properties", "instance": "", "value": [ "foo", "permitted" ] }
{ "foo": "bar", "baz": 2 }
{ "keyword": "/properties", "instance": "", "value": [ "foo", "baz" ] }
{ "forbidden": 1 }
{ "forbidden": 1, "permitted": 2 }