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 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" ]
  }
}
An instance with both 'age' and 'license' properties is valid Instance
{
  "name": "John",
  "age": 25,
  "license": "XYZ123"
}
An instance with missing 'age' property when 'license' property is present is invalid Instance
{
  "name": "John",
  "license": "XYZ123"
}
An instance without 'license' property is valid Instance
{
  "name": "John",
  "age": 25
}
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" ]
  }
}
An instance with all the dependent properties is valid Instance
{
  "productName": "Iphone",
  "productPriceUSD": 399.99,
  "units": 5,
  "totalCost": 1599.99,
  "trackingId" : 1414326241,
  "outForDelivery": "yes"
}
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"
}
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.