dependentRequired : Object<String, Array<String>>
dependentRequired
Object<String, Array<String>>Validation succeeds if, for each name that appears in both the instance and as a name within this keyword’s value, every item in the corresponding array is also the name of a property in the instance.
Kind | Assertion |
---|---|
Applies To | Object |
Dialect | 2020-12 |
Introduced In | 2019-09 |
Vocabulary | Validation |
Specification | https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.4 |
Metaschema | https://json-schema.org/draft/2020-12/meta/validation |
Also see |
|
The dependentRequired
keyword specifies a conditional dependency between properties within an instance. It ensures that if a certain property is present in an instance, then another specified set of properties must also be present. In short, if property A exists in an instance, then properties B, C, and D must also be present.
- The value of this keyword must be an object.
- Properties in this object, if any, must be arrays.
- Items in each array, if any, must be strings, and must be unique.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"license": { "type": "string" }
},
"dependentRequired": {
"license": [ "age" ]
}
}
{
"name": "John",
"age": 25,
"license": "XYZ123"
}
{
"name": "John",
"license": "XYZ123"
}
{
"name": "John",
"age": 25
}
{}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"productName": { "type": "string" },
"productPriceUSD": { "type": "number" },
"units": { "type": "number" }
},
"dependentRequired": {
"productPriceUSD": [ "productName" ],
"totalCost" : [ "productPriceUSD", "units" ],
"trackingId": [ "outForDelivery" ]
}
}
{
"productName": "Iphone",
"productPriceUSD": 399.99,
"units": 5,
"totalCost": 1599.99,
"trackingId" : 1414326241,
"outForDelivery": "yes"
}
{
"productName": "Iphone",
"units": 5,
"totalCost": 1599.99,
"trackingId" : 1414326241,
"outForDelivery": "yes"
}
{
"productName": "Iphone",
"productPriceUSD": 399.99
}
// The 'totalCost' property is not present in this instance, so it will be valid regardless of the presence of 'units' or 'productPriceUSD' property.