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.