Looking to master JSON Schema for OpenAPI?

A 9+ hour course taught by a member of the JSON Schema Technical Steering Committee

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, dependencies restricts 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, dependencies restricts 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.

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

A schema that constrains object instances with a single property dependency Schema
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "dependencies": {
    "foo": [ "bar", "baz" ]
  }
}
Valid An object value that defines the dependency and all of the required properties is valid Instance
{ "foo": 1, "bar": 2, "baz": 3 }
Invalid An object value that defines the dependency and some of the required properties is invalid Instance
{ "foo": 1, "bar": 2 }
Invalid An object value that defines the dependency and none of the required properties is invalid Instance
{ "foo": 1 }
Valid An object value that does not define the dependency is valid Instance
{ "qux": 4 }
Valid An empty object value is valid as no dependencies apply Instance
{}
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances with transitive property dependencies Schema
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "dependencies": {
    "foo": [ "bar" ],
    "bar": [ "baz" ]
  }
}
Valid An object value that satisfies the transitive dependency is valid Instance
{ "foo": 1, "bar": 2, "baz": 3 }
Invalid An object value that only satisfies the first part of the transitive dependency is invalid Instance
{ "foo": 1, "bar": 2 }
Valid An object value that only satisfies the second part of the transitive dependency is valid Instance
{ "bar": 2, "baz": 3 }
Valid An empty object value is valid as no dependencies apply Instance
{}
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances with a single schema dependency Schema
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "dependencies": {
    "foo": { "maxProperties": 2 }
  }
}
Valid An object value that defines the property dependency and matches the dependent schema is valid Instance
{ "foo": 1, "bar": 2 }
Invalid An object value that defines the property dependency but does not match the dependent schema is invalid Instance
{ "foo": 1, "bar": 2, "baz": 3 }
Valid An object value that does not define the property dependency is valid Instance
{ "firstName": "John", "lastName": "Doe", "age": 50 }
Valid An empty object value is valid as no dependencies apply Instance
{}
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances with multiple schema dependencies Schema
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "dependencies": {
    "foo": { "maxProperties": 2 },
    "bar": { "minProperties": 2 }
  }
}
Valid An object value that defines both property dependencies and has exactly 2 properties is valid Instance
{ "foo": 1, "bar": 2 }
Invalid An object value that defines both property dependencies but has more than 2 properties is invalid Instance
{ "foo": 1, "bar": 2, "extra": true }
Valid An object value that defines the first property dependency and has less than 2 properties is valid Instance
{ "foo": 1 }
Invalid An object value that defines the first property dependency and has more than 2 properties is invalid Instance
{ "foo": 1, "name": "John Doe", "age": 50 }
Invalid An object value that defines the second property dependency and has less than 2 properties is invalid Instance
{ "bar": 2 }
Valid An empty object value is valid as no dependencies apply Instance
{}
Valid A non-object value is valid Instance
"Hello World"
A schema that combines property and schema dependencies Schema
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "dependencies": {
    "creditCard": [ "billingAddress" ],
    "billingAddress": { "required": [ "street", "city", "zipcode" ] }
  }
}
Valid An object value that defines the credit card with billing address having all required fields is valid Instance
{
  "creditCard": "1234-5678-9012-3456",
  "billingAddress": {
    "street": "123 Main St",
    "city": "Anytown",
    "zipcode": "12345"
  }
}
Invalid An object value that defines the credit card but missing the billing address is invalid Instance
{ "creditCard": "1234-5678-9012-3456" }
Invalid An object value with billing address that does not have all required fields is invalid Instance
{
  "billingAddress": {
    "street": "123 Main St"
  }
}
Valid An object value that defines neither dependency is valid Instance
{ "name": "John Doe" }
Valid An empty object value is valid as no dependencies apply Instance
{}
Valid A non-object value is valid Instance
"Hello World"