user8479984
user8479984

Reputation: 501

JSON Schema not validating required attributes

Here is the criteria: the individual region object can co-exist withing the array along with ONLY asia/europe/austrlia (either one region at a time from asia/europe/austrlia).

In addition to this every region object can have few required attributes and nested one.

The issue is the schema validator is not complaining about the required attribute (i.e. width attribute from dimension object)

Here is the JSON Schema

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "stat_data": {
      "type": "array",
      "if": {
        "contains": {
          "type": "object",
          "properties": {
            "region": {
              "type": "string",
              "enum": [
                "europe"
              ]
            }
          }
        }
      },
      "then": {
        "not": {
          "contains": {
            "type": "object",
            "properties": {
              "region": {
                "type": "string",
                "enum": [
                  "asia",
                  "australia"
                ]
              }
            }
          }
        }
      },
      "else": {
        "if": {
          "contains": {
            "type": "object",
            "properties": {
              "region": {
                "type": "string",
                "enum": [
                  "asia"
                ]
              }
            }
          }
        },
        "then": {
          "not": {
            "contains": {
              "type": "object",
              "properties": {
                "region": {
                  "type": "string",
                  "enum": [
                    "europe",
                    "australia"
                  ]
                }
              }
            }
          }
        },
        "else": {
          "if": {
            "contains": {
              "type": "object",
              "properties": {
                "region": {
                  "type": "string",
                  "enum": [
                    "australia"
                  ]
                }
              }
            }
          },
          "then": {
            "not": {
              "contains": {
                "type": "object",
                "properties": {
                  "region": {
                    "type": "string",
                    "enum": [
                      "europe",
                      "asia"
                    ]
                  }
                }
              }
            }
          },
          "else": {}
        }
      },
      "items": {
        "type": "object",
        "properties": {
          "details": {
            "$ref": "#/definitions/dimension"
          },
          "population": {
            "$ref": "#/definitions/details"
          },
          "dimension": {
            "$ref": "#/definitions/population"
          },
          "region": {
            "enum": [
              "asia",
              "europe",
              "australia",
              "some-pencil-region",
              "some-oil-pastels-region"
            ]
          }
        }
      }
    }
  },
  "definitions": {
    "dimension": {
      "type": "object",
      "required": [
        "width"
      ],
      "properties": {
        "height": {
          "type": "integer"
        },
        "width": {
          "type": "integer"
        }
      }
    },
    "details": {
      "type": "object",
      "properties": {
        "brand": {
          "type": "string"
        },
        "year": {
          "type": "integer"
        }
      }
    },
    "population": {
      "type": "object",
      "properties": {
        "change": {
          "type": "integer"
        },
        "year": {
          "type": "integer"
        }
      }
    }
  }
}

and the JSON

{
  "stat_data": [
    {
      "region": "some-pencil-region",
      "details": {
        "brand": "Camlin",
        "year": 2019
      }
    },
    {
      "region": "some-oil-pastels-region",
      "height": 30
    },
    {
      "region": "asia",
      "population": {
        "year": 2018,
        "change": 2
      }
    }
  ]
}

You can check at _https://jsonschema.dev/ by copying schema and json in the editor

Upvotes: 0

Views: 272

Answers (1)

Clemens
Clemens

Reputation: 1817

Maybe your JSON validator doesn't catch the issue? JSONBuddy shows a message about a missing "width" property:

enter image description here

Upvotes: 1

Related Questions