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": "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" ]
}
}
}
{
"name": "John",
"age": 25,
"license": "XYZ123"
}
{
"name": "John",
"age": "25",
"license": "XYZ123"
}
{
"name": "John",
"age": "25"
}
{
"name": "John",
"license": "XYZ123"
}
{
"$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" ]
}
}
}
{
"name": "John",
"age": 15,
"eligible": false
}
{
"name": "manager",
"age": "25",
"eligible": true
}
{
"age": "25",
"eligible": true
}
{
"age": "25",
"eligible": true
}
{
"name": "manager",
"age": "25"
}