dependencies : Object<String, Array<String> | Schema>
dependencies
Object<String, Array<String> | Schema>Validation succeeds if, for each name that appears in both the instance and as a name within this keyword’s value, either every item in the corresponding array is also the name of a property in the instance or the corresponding subschema successfully evaluates against the instance.
| Value |
This keyword must be set to an object where each value is either an array of unique strings or a valid JSON Schema
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
|---|---|
| Kind | Assertion |
| Applies To | Object |
| Base Dialect | Draft 4 |
| Changed In | 2019-09 |
| Introduced In | Draft 3 |
| Vocabulary | Validation |
| Specification | https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.5.4.5 |
| Metaschema | http://json-schema.org/draft-04/schema# |
| Official Tests | draft4/dependencies.json |
| Default |
{}
|
| Annotation | None |
| Affected By | None |
| Affects | None |
| Also See |
|
The dependencies keyword is
used to express property-based constraints on object instances. It has two
different modes of operation depending on the type of each dependency value:
-
Property Dependencies (Array): When a dependency value is set to an array of strings,
dependenciesrestricts object instances to define certain properties if the corresponding property key is also defined. -
Schema Dependencies (Schema): When a dependency value is set to a schema,
dependenciesrestricts object instances to validate against the given subschema if the corresponding property key is defined. Note that the given subschema is evaluated against the object that defines the property dependency.
Common Pitfall
Note that multiple potentially interrelated dependencies
can be declared at once, in which case every dependency must be transitively
fulfilled for the object instance to be valid. For example, if a schema marks
the property B as required if the property A is present and also marks the
property C as required if the property B is present, defining the property
A transitively requires both the B and C properties to be present in
the object instance.
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": "http://json-schema.org/draft-04/schema#",
"dependencies": {
"foo": [ "bar", "baz" ]
}
}{ "foo": 1, "bar": 2, "baz": 3 }{ "foo": 1, "bar": 2 }{ "foo": 1 }{ "qux": 4 }{}"Hello World"{
"$schema": "http://json-schema.org/draft-04/schema#",
"dependencies": {
"foo": [ "bar" ],
"bar": [ "baz" ]
}
}{ "foo": 1, "bar": 2, "baz": 3 }{ "foo": 1, "bar": 2 }{ "bar": 2, "baz": 3 }{}"Hello World"{
"$schema": "http://json-schema.org/draft-04/schema#",
"dependencies": {
"foo": { "maxProperties": 2 }
}
}{ "foo": 1, "bar": 2 }{ "foo": 1, "bar": 2, "baz": 3 }{ "firstName": "John", "lastName": "Doe", "age": 50 }{}"Hello World"{
"$schema": "http://json-schema.org/draft-04/schema#",
"dependencies": {
"foo": { "maxProperties": 2 },
"bar": { "minProperties": 2 }
}
}{ "foo": 1, "bar": 2 }{ "foo": 1, "bar": 2, "extra": true }{ "foo": 1 }{ "foo": 1, "name": "John Doe", "age": 50 }{ "bar": 2 }{}"Hello World"{
"$schema": "http://json-schema.org/draft-04/schema#",
"dependencies": {
"creditCard": [ "billingAddress" ],
"billingAddress": { "required": [ "street", "city", "zipcode" ] }
}
}{
"creditCard": "1234-5678-9012-3456",
"billingAddress": {
"street": "123 Main St",
"city": "Anytown",
"zipcode": "12345"
}
}{ "creditCard": "1234-5678-9012-3456" }{
"billingAddress": {
"street": "123 Main St"
}
}{ "name": "John Doe" }{}"Hello World"