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.

Kind Applicator
Applies To Object
Dialect 2020-12
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
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.

  • This keyword’s value must be an object.
  • Each key in the object represents a property name.
  • Each value in the object must be a valid JSON Schema.
  • 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" ]
    }
  }
}
An instance with both 'age' and 'license' properties is valid Instance
{
  "name": "John",
  "age": 25,
  "license": "XYZ123"
}
An instance with the 'age' property not conforming to the subschema is invalid Instance
{
  "name": "John",
  "age": "25",
  "license": "XYZ123"
}
When the 'license' property is missing, the 'age' property will not affect the validation Instance
{
  "name": "John",
  "age": "25"
}
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" ]
    }
  }
}
An instance with all the required properties is valid Instance
{
  "name": "John",
  "age": 15,
  "eligible": false
}
An instance with 'age' property not conforming to the schema is invalid Instance
{
  "name": "manager",
  "age": "25",
  "eligible": true
}
'age' and 'eligible' properties do not affect the validation when the 'name' property is missing Instance
{
  "age": "25",
  "eligible": true
}
'age' and 'eligible' properties do not affect the validation when the 'name' property is missing Instance
{
  "age": "25",
  "eligible": true
}
An instance with missing 'eligible' property when 'age' property is present is invalid Instance
{
  "name": "manager",
  "age": "25"
}