dependentSchemas : Object<String, Schema>

dependentSchemas

Object<String, Schema>

This keyword specifies subschemas that are evaluated if the instance is an object and contains a certain property.

Value This keyword must be set to an object where each value is a valid JSON Schema
Kind Applicator
Applies To Object
Base Dialect 2020-12
Changed In None
Introduced In 2019-09
Vocabulary Applicator
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.2.4
Metaschema https://json-schema.org/draft/2020-12/meta/applicator
Official Tests draft2020-12/dependentSchemas.json
Default {}
Annotation None
Affected By None
Affects None
Also See

The dependentSchemas keyword restricts object instances to validate against one or more of the given subschemas if the corresponding properties are defined. Note that the given subschemas are 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 schema dependency Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "dependentSchemas": {
    "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 property schema dependencies Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "dependentSchemas": {
    "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"