You can do this with a boolean logic concept called implication (!A or B). It can be used like an "if-then" statement. For example, either "color" is not "red" or "redQuote" is required. Any time I need to use this, I break it down with definitions so it reads as nice as possible.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "colour": { "enum": ["red", "black", "blue"] },
    "blackQuote": { "type": "string", "maxLength": 11 },
    "redQuote": { "type": "string", "maxLength": 11 }
  },
  "allOf": [
    { "$ref": "#/definitions/red-requires-redQuote" },
    { "$ref": "#/definitions/black-requires-blackQuote" }
  ],
  "required": ["colour"],
  "definitions": {
    "red-requires-redQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-red" } },
        { "required": ["redQuote"] }
      ]
    },
    "black-requires-blackQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-black" } },
        { "required": ["blackQuote"] }
      ]
    },
    "is-red": {
      "properties": {
        "colour": { "enum": ["red"] }
      },
      "required": ["colour"]
    },
    "is-black": {
      "properties": {
        "colour": { "enum": ["black"] }
      },
      "required": ["colour"]
    }
  }
}
Answer from Jason Desrosiers on Stack Overflow
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › conditionals
JSON Schema - Conditional schema validation
The dependentRequired keyword conditionally requires that certain properties must be present if a given property is present in an object. For example, suppose we have a schema representing a customer. If you have their credit card number, you also want to ensure you have a billing address.
Top answer
1 of 3
14

You can do this with a boolean logic concept called implication (!A or B). It can be used like an "if-then" statement. For example, either "color" is not "red" or "redQuote" is required. Any time I need to use this, I break it down with definitions so it reads as nice as possible.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "colour": { "enum": ["red", "black", "blue"] },
    "blackQuote": { "type": "string", "maxLength": 11 },
    "redQuote": { "type": "string", "maxLength": 11 }
  },
  "allOf": [
    { "$ref": "#/definitions/red-requires-redQuote" },
    { "$ref": "#/definitions/black-requires-blackQuote" }
  ],
  "required": ["colour"],
  "definitions": {
    "red-requires-redQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-red" } },
        { "required": ["redQuote"] }
      ]
    },
    "black-requires-blackQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-black" } },
        { "required": ["blackQuote"] }
      ]
    },
    "is-red": {
      "properties": {
        "colour": { "enum": ["red"] }
      },
      "required": ["colour"]
    },
    "is-black": {
      "properties": {
        "colour": { "enum": ["black"] }
      },
      "required": ["colour"]
    }
  }
}
2 of 3
6

Simplest answer in draft-04 (as noted by Ganesh in a comment):

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {

    "colour": {
      "type": "string",
      "enum": ["red", "black", "blue"]
    },

    "blackQuote": {
      "type": "string",
      "maxLength": 11
    },

    "redQuote": {
      "type": "string",
      "maxLength": 11
    }
  },

  "oneOf": [
      {
        "properties": {
          "colour": {"enum": ["red"]}
        },
        "required": ["redQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["black"]}
        },
        "required": ["blackQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["blue"]}
        }
      }
  ],

  "required": [
    "colour"
  ]
}
Discussions

jsonschema - Json Schema Validation: Dependencies on subschema - Stack Overflow
I am new to json schema validation and I am having trouble validating a required field based on the existence and value of a field deeper in the json Below is my current schema and an example json... More on stackoverflow.com
🌐 stackoverflow.com
Proposal: Add propertyDependencies keyword (aka discriminator)
Compared to discriminator, this is more consistent with the style of JSON Schema keywords because it doesn't use sub-keywords like propertyName and mappings. It's also more powerful because it allows you to discriminate using more than one property. Because of the parallels to dependencies, I chose ... More on github.com
🌐 github.com
38
March 23, 2021
dependencies - JSON Schema - specify field is required based on value of another field - Stack Overflow
It's a shame there isn't better ... examples for each part of the spec. 2014-10-07T08:51:13.697Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Results of the October 2025 Community Asks Sprint: copy button for code... Chat room owners can now establish room guidelines ... 2 JSON Schema Dependencies remove key ... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to have "dependencies" based on values instead of presence?
json-schema / json-schema Public ... kriszyp/json-schema ... This repository was archived by the owner on Mar 19, 2019. It is now read-only. ... if (drink.name == 'beer' || drink.name == 'wine') // "color" is a required fiels else if (drink.name == "water") // nothing is required · Maybe i'm wrong but I can't find a way to implement this case, dependencies keyword seems ... More on github.com
🌐 github.com
3
March 25, 2015
🌐
Readthedocs
react-jsonschema-form.readthedocs.io › en › v1.8.1 › dependencies
Dependencies - react-jsonschema-form documentation
{ "type": "object", "properties": { "name": { "type": "string" }, "credit_card": { "type": "number" } }, "required": ["name"], "dependencies": { "credit_card": { "properties": { "billing_address": { "type": "string" } }, "required": ["billing_address"] } } } In this example the billing_address field will be displayed in the form if credit_card is defined. (Sample schemas courtesy of the Space Telescope Science Institute) The JSON Schema standard says that the dependency is triggered if the property is present.
🌐
MongoDB
mongodb.com › blog › post › json-schema-validation--dependencies-you-can-depend-on
JSON Schema Validation - Dependencies you can depend on | MongoDB Blog
November 18, 2019 - An example use case of property dependencies would be students. If a student has graduated from a program, we want to stay in touch with them and want their mailing address to be a required field.
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › validation › dependentrequired
dependentRequired (2020-12) - Learn JSON Schema
A schema that constrains object instances with a single property dependency Schema ... { "$schema": "https://json-schema.org/draft/2020-12/schema", "dependentRequired": { "foo": [ "bar", "baz" ] } }
🌐
Liquid Technologies
liquid-technologies.com › Reference › XmlStudio › JsonEditorNotation_SchemaDependency.html
Schema Dependency
The object must validate against the schema in the dependency, which contains: A Properties Collection which contains an additional properties node (this allows any properties so has no effect on the validation). A Not schema (which is not allowed to validate against the containing instance object), this contains:
🌐
JSON Schema
json-schema.org › learn › miscellaneous-examples
JSON Schema - Miscellaneous Examples
The given schema showcases the use of the dependentSchemas keyword. It allows defining a subschema that must be satisfied if a certain property is present. In this example, the schema defines an object with two properties: foo and propertiesCount.
🌐
Rjsf-team
rjsf-team.github.io › json schema › dependencies
Dependencies | react-jsonschema-form
import { RJSFSchema } from ... courtesy of the Space Telescope Science Institute) The JSON Schema standard says that the dependency is triggered if the property is present....
Find elsewhere
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › dependentschemas
dependentSchemas (2020-12) - Learn JSON Schema
A schema that constrains object instances with a single property schema dependency Schema ... { "$schema": "https://json-schema.org/draft/2020-12/schema", "dependentSchemas": { "foo": { "maxProperties": 2 } } }
Top answer
1 of 1
1

Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?

Solution

You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.

Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd recommend to read following:

JSON Schema: How to check that an array contains at least one object with a property with a given value?

Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)

https://json-schema.org/understanding-json-schema/reference/array.html

Your schema re-worked below. See the "definitions" section

First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.

"definitions" : {
    "Fields-contains-at-least-1-element-with-SW-Large-true" : {
      "properties": { 
        "Fields" : {
          "contains" : {
            "properties": { 
              "SW Large": { "enum": [true] } 
            },
            "required" : ["SW Large"]
          }
        } 
      },
    }
  },

If you would add it permanently, it might look like:

"Fields" : {
  "type" : "array",
  "contains" : {
    "properties": { 
      "SW Large": { "enum": [true] } 
    },
    "required" : ["SW Large"]
  }
  "items": {...},
}

which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:

"Fields" : {
    "type" : "array",
    "contains" : {
      "not" : {
        "properties": { 
          "SW Large": { "enum": [true] } 
        },
        "required" : ["SW Large"]
      }
    }
    "items": {...},
  }

it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.

I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.

The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword

It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required

"definitions" : {
    "upward-dependency" : {
      "anyOf" : [
        { "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
        { "required" : ["SW Words"] }
      ]
    },
  },

At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!

       "items" : {
        "$id": "#/properties/Registers/items
        "type" : "object",
        "properties" : {
          "Fields": {
            "$id": "#/properties/Registers/items/properties/Fields",
            "type": "array",
            "items": {
              "$id": "#/properties/Registers/items/properties/Fields/items",
              "type": "object",
              "propertyNames" : {
                "enum" : [
                  "Name",
                  "SW Large"
                ]                
              },
              "required": [
                "Name"
              ],
              "properties": {
                "Name": {
                  "$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
                  "type": "string"
                },
                "SW Large": {
                  "$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
                  "type": "boolean"
                }
              }
            }
          }
        },
        "dependencies" : {
          "Fields" : { "$ref" : "#/definitions/upward-dependency" }
        },
      }

I've checked Schema with online validator and added your object to "examples" section.

Full schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "propertyNames" : {
    "enum" : [
      "Registers"
    ]
  },
  "required": [
    "Registers"
  ],
  "examples" : [
    {
      "Registers": [
        {
          "Name": "device",
          "Address": "100",
          "SW Words": 2,
          "Fields": [
            {
              "Name" : "Product",
              "SW Large" : true
            },
            {
              "Name": "Version"
            }
          ]
        }
      ]
    }
  ],
  "properties": {
    "Registers": {
      "$id": "#/properties/Registers",
      "type": "array",
      "items": {
        "$id": "#/properties/Registers/items",
        "type": "object",
        "propertyNames" : {
          "enum" : [
            "Name",
            "Address",
            "SW Words",
            "Fields"
          ]
        },
        "required": [
          "Name",
          "Address",
          "Fields"
        ],
        "properties": {
          "Name": {
            "$id": "#/properties/Registers/items/properties/Name",
            "type": "string"
          },
          "Address": {
            "$id": "#/properties/Registers/items/properties/Address",
            "type": "string"
          },
          "SW Words": {
            "$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
            "type": "integer",
            "default": 2,
            "minimum": 2
          },
          "Fields": {
            "$id": "#/properties/Registers/items/properties/Fields",
            "type": "array",
            "items": {
              "$id": "#/properties/Registers/items/properties/Fields/items",
              "type": "object",
              "propertyNames" : {
                "enum" : [
                  "Name",
                  "SW Large"
                ]                
              },
              "required": [
                "Name"
              ],
              "properties": {
                "Name": {
                  "type": "string"
                },
                "SW Large": {
                  "type": "boolean"
                }
              }
            }
          }
        },
        "dependencies" : {
          "Fields" : { "$ref" : "#/definitions/upward-dependency" }
        },
      }
    }
  },
  "definitions" : {
    "upward-dependency" : {
      "anyOf" : [
        { "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
        { "required" : ["SW Words"] }
      ]
    },
    "Fields-contains-at-least-1-element-with-SW-Large-true" : {
      "properties": { 
        "Fields" : {
          "contains" : {
            "properties": { 
              "SW Large": { "enum": [true] } 
            },
            "required" : ["SW Large"]
          }
        } 
      },
    }
  },
}

Some side notes:

Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.

"propertyNames" : {
    "enum" : [
      "Registers"
    ]
  }

See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names

It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .

If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.

Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html

I hope it helped.

🌐
To The New
tothenew.com › home › using "dependencies" in json schema (version : draft-v4)
Using “dependencies” in json schema (version : draft-v4) | TO THE NEW Blog
March 22, 2016 - [code] { "title": "Test dependncies", "readOnly": false, "$schema": "http://json-schema.org/draft-04/hyper-schema", "description": "This a test to show how we can use dependencies in json schema", "properties":{ "graduate": { "title":"Graduate?", "type":"string", "enum":["Yes","No"] }, "minimumEligibilityAge":{ "title":"Enter Age", "type":"number" }, "courseName":{ "title":"Enter Graduation Course Name", "type":"string" } }, "type": "object", "required":[ "graduate" ] } [/code]
🌐
Ajv
ajv.js.org › json-schema.html
JSON Schema | Ajv JSON schema validator
The value of the keyword is a map with keys equal to data object properties. Each value in the map should be either an array of unique property names ("property dependency" - see dependentRequired keyword) or a JSON Schema ("schema dependency" - see dependentSchemas keyword).
🌐
Rjsf-team
rjsf-team.github.io › json schema
JSON Schema | react-jsonschema-form
react-jsonschema-form supports ... schemas that change fields based on what data is entered. ... The simplest example of a JSON Schema contains only a single field....
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 1082
Proposal: Add propertyDependencies keyword (aka discriminator) · Issue #1082 · json-schema-org/json-schema-spec
March 23, 2021 - Right now, we have the dependentSchemas keyword that is very close to what is needed except it checks for the presence of a property rather than it's value.
Author   jdesrosiers
🌐
Learnjsonschema
learnjsonschema.com › draft4 › validation › dependencies
dependencies (Draft 4) - Learn JSON Schema
Note that multiple potentially ... every dependency must be transitively fulfilled for the object instance to be valid. For example, if a schema marks the property B as required if the property A is present and also marks the property C as required if the property B is present, defining the property A transitively requires both the B and C properties to be present in the object instance. Remember that JSON Schema is ...
🌐
Google Groups
groups.google.com › g › json-schema › c › e4xVu24JUgM
JSON Schema Validation - dependencies example
I'm not sure if this is the correct use. But here is the case. We are trying to specify validation rules based on values. In this case is different combinations of values may be valid. What I am trying to say by the oneOf is that one of these dependencies should be true.
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › object
JSON Schema - object
You can use additionalProperties with a combination of properties and patternProperties. In the following example, based on the example from patternProperties, we add a "builtin" property, which must be a number, and declare that all additional properties (that are neither defined by properties nor matched by patternProperties) must be strings:
🌐
GitHub
github.com › json-schema › json-schema › issues › 158
Is it possible to have "dependencies" based on values instead of presence? · Issue #158 · json-schema/json-schema
March 25, 2015 - { "type": "object", "properties": { "name": { "type": "string", "enum": ["beer", "wine", "water"] }, "color" : { "type": "string" } }, "required": ["name"], "dependencies": { "name": ["color"] // I can't specify the value of name } }
Author   marqu3z
🌐
Learnjsonschema
learnjsonschema.com › draft7 › validation › dependencies
dependencies (Draft 7) - Learn JSON Schema
Note that multiple potentially ... every dependency must be transitively fulfilled for the object instance to be valid. For example, if a schema marks the property B as required if the property A is present and also marks the property C as required if the property B is present, defining the property A transitively requires both the B and C properties to be present in the object instance. Remember that JSON Schema is ...