0

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. I need to have it say if "SW Large" exist and the value is true then it requires that "SW Words" higher exist within the Register object

The "anyOf" I have in my below schema was my attempt.

It works to say if "SW Large" is true then "SW Words" is required else not but if the field "SW Large" is not present it still will insist that "SW Words" is required.

How can I change the "anyOf" and "allOf" to check if "SW Large" actually exists?

Edit: Stripping back the json to be a bit more manageable

Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "Registers"
  ],
  "properties": {
    "Registers": {
      "$id": "#/properties/Registers",
      "type": "array",
      "items": {
        "$id": "#/properties/Registers/items",
        "type": "object",
        "additionalProperties": false,
        "required": [
          "Name",
          "Address",
          "Fields"
        ],
        "anyOf": [
          {
            "allOf": [
              { "not": { "properties": { "Fields" : { "items": { "properties": { "SW Large": { "const": true } } } } } } }
            ]
          },
          {
            "required": ["SW Words"]
          }
        ],
        "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/properties/Registers/items/properties/Fields",
            "type": "array",
            "items": {
              "$id": "#/properties/Registers/items/properties/Fields/items",
              "type": "object",
              "additionalProperties": false,
              "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"
                }
              }
            }
          }
        }
      }
    }
  }
}

Example Json

{
  "Registers": [
    {
      "Name": "device",
      "Address": "100",
      "SW Words": 2,
      "Fields": [
        {
          "Name" : "Product",
          "SW Large" : true
        },
        {
          "Name": "Version"
        }
      ]
    }
  ]
}
Adam
  • 3
  • 1
  • 3

1 Answers1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PsychoFish
  • 1,541
  • 12
  • 11
  • You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics! – Adam Nov 15 '18 at 17:26
  • Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through https://stackoverflow.com/questions/48015822/json-schema-require-a-property-only-when-a-specific-property-is-present-in-a-de?rq=1 - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow? – PsychoFish Nov 15 '18 at 19:37
  • I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help – Adam Nov 16 '18 at 10:00
  • Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? https://json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators) – PsychoFish Nov 16 '18 at 11:41
  • I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools – Adam Nov 16 '18 at 12:41
  • Play around it - I suspect something's wrong in python jsonschema resolving "dependencies" keyword (this is conditional requirement). If I get some time, I'll try to shape it w/o usage of "dependencies" keyword and will let you know, but no promises - kids can eat up all the after hours time in the world ;-) – PsychoFish Nov 16 '18 at 14:00