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.
Value | This keyword must be set to an object where each value is an array of unique strings |
---|---|
Kind | Assertion |
Applies To | Object |
Dialect | 2020-12 |
Changed In | None |
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 |
Official Tests | draft2020-12/dependentRequired.json |
Default |
{}
|
Annotation | None |
Affected By | None |
Affects | None |
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.
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.