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
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 allows you to define dependencies between properties based on the presence of other properties within an instance. It extends the functionality of the dependentRequired keyword by allowing you to pass in a full schema. The instance will be considered valid only if the dependent properties adhere to the dependentSchemas schema.

  • Each key in the object represents a property name.
  • Instance is valid if the associated property is present and conforms to the subschema.

Examples

Schema with the 'dependentSchemas' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "license": { "type": "string" }
  },
  "dependentSchemas": {
    "license": {
      "properties": {
        "age": { "type": "number" }
      },
      "required": [ "age" ]
    }
  }
}
Valid An instance with both 'age' and 'license' properties is valid Instance
{
  "name": "John",
  "age": 25,
  "license": "XYZ123"
}
Invalid An instance with the 'age' property not conforming to the subschema is invalid Instance
{
  "name": "John",
  "age": "25",
  "license": "XYZ123"
}
Valid When the 'license' property is missing, the 'age' property will not affect the validation Instance
{
  "name": "John",
  "age": "25"
}
Invalid An instance with missing 'age' property when 'license' property is present is invalid Instance
{
  "name": "John",
  "license": "XYZ123"
}
Complex schema with the 'dependentSchemas' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" }
  },
  "dependentSchemas": {
    "name": {
      "properties": {
        "age": { "type": "number" }
      },
      "dependentSchemas": {
        "age": {
          "properties": {
            "eligible": { "type": "boolean" }
          },
          "required": [ "eligible" ]
        }
      },
      "required": [ "age" ]
    }
  }
}
Valid An instance with all the required properties is valid Instance
{
  "name": "John",
  "age": 15,
  "eligible": false
}
Invalid An instance with 'age' property not conforming to the schema is invalid Instance
{
  "name": "manager",
  "age": "25",
  "eligible": true
}
Valid 'age' and 'eligible' properties do not affect the validation when the 'name' property is missing Instance
{
  "age": "25",
  "eligible": true
}
Valid 'age' and 'eligible' properties do not affect the validation when the 'name' property is missing Instance
{
  "age": "25",
  "eligible": true
}
Invalid An instance with missing 'eligible' property when 'age' property is present is invalid Instance
{
  "name": "manager",
  "age": "25"
}