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 with the 'dependentRequired' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "license": { "type": "string" }
  },
  "dependentRequired": {
    "license": [ "age" ]
  }
}
Valid An instance with both 'age' and 'license' properties is valid Instance
{
  "name": "John",
  "age": 25,
  "license": "XYZ123"
}
Invalid An instance with missing 'age' property when 'license' property is present is invalid Instance
{
  "name": "John",
  "license": "XYZ123"
}
Valid An instance without 'license' property is valid Instance
{
  "name": "John",
  "age": 25
}
Valid An empty object is also valid Instance
{}
Complex schema with the 'dependentRequired' keyword Schema
{
  "$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" ]
  }
}
Valid An instance with all the dependent properties is valid Instance
{
  "productName": "Iphone",
  "productPriceUSD": 399.99,
  "units": 5,
  "totalCost": 1599.99,
  "trackingId" : 1414326241,
  "outForDelivery": "yes"
}
Invalid An instance with missing 'productPriceUSD' property when 'totalCost' property is present is invalid Instance
{
  "productName": "Iphone",
  "units": 5,
  "totalCost": 1599.99,
  "trackingId" : 1414326241,
  "outForDelivery": "yes"
}
Valid An instance with 'productName' and 'productPriceUSD' is valid Instance
{
  "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.