user8479984
user8479984

Reputation: 501

Nested allOf, anyOf, oneOf in json schema

Here is my revised problem and I am borrowing the same learning that I have learnt in one of the question. I have added my schema & JSON at the bottom of this question.

Criteria:

  1. Each region object will always have an attribute as 'region' and could have other different attributes and a nested json object too. (Please note the object doesn not have similar attribute and so I am using definitions)
  2. The array must contain atleast one region object but could be ONLY of type: australia or asia or europe ALONG with other region type of object. [ australia or asia or europe cannot coexist with each other]
  3. The JSON Schema should complain about the missing required attribute.

So this condition is valid:

Here the array is ["stat_data":{},{},{}]

  1. [{"asia"}] //or europe or australia
  2. [{"some-pencil-region"},{"asia"}]
  3. [{"some-pencil-region"},{some-oil-pastels-region}, {"asia"}]
  4. [{some-oil-pastels-region}, {"europe"}]
  5. [{"some-pencil-region"}, {"europe"}]

and this condition is invalid:

  1. []
  2. [{"some-pencil-region"},{"asia"},{"europe"} {australia}] //asia, europe, australia cannot co-exit
  3. [{some-oil-pastels-region},{"some-pencil-region"},{"asia"},{"asia"}, {australia}] //asia, europe, australia cannot co-exit
  4. [{"some-pencil-region"}] //Missing: Either asia OR europe OR australia should be there along with other object

JSON Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Pencils": {
      "contains": {
        "properties": {
          "region": {
            "const": "some-pencil-region"
          },
          "details": {
            "type": "object",
            "required": [
              "brand",
              "year"
            ],
            "properties": {
              "brand": {
                "type": "string"
              },
              "year": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "OilPastels": {
      "contains": {
        "required": [
          "population"
        ],
        "properties": {
          "region": {
            "const": "some-oil-pastels-region"
          },
          "details": {
            "type": "object",
            "properties": {
              "size": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "containsAsia": {
      "contains": {
        "required": [
          "population"
        ],
        "properties": {
          "region": {
            "const": "asia"
          },
          "population": {
            "type": "object",
            "required": [
              "year"
            ],
            "properties": {
              "year": {
                "type": "number"
              },
              "change": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "containsEurope": {
      "contains": {
        "properties": {
          "region": {
            "const": "europe"
          },
          "tourist": {
            "type": "number"
          }
        }
      }
    },
    "containsAustralia": {
      "contains": {
        "properties": {
          "region": {
            "const": "australia"
          },
          "stadium": {
            "type": "object",
            "required": [
              "year"
            ],
            "properties": {
              "year": {
                "type": "number"
              },
              "area": {
                "type": "number"
              }
            }
          }
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "stat_data": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object"
      },
      "oneOf": [
        {
          "$ref": "#/definitions/Pencils"
        },
        {
          "$ref": "#/definitions/OilPastels"
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsAsia"
            },
            {
              "not": {
                "$ref": "#/definitions/containsEurope"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsAustralia"
              }
            }
          ]
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsEurope"
            },
            {
              "not": {
                "$ref": "#/definitions/containsAsia"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsAustralia"
              }
            }
          ]
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsAustralia"
            },
            {
              "not": {
                "$ref": "#/definitions/containsAsia"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsEurope"
              }
            }
          ]
        }
      ]
    }
  }
}

JSON (This is failing) [I tried all my validation but all vain]

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

10/06 Not validating the mandatory attribute LINK1

Upvotes: 1

Views: 4083

Answers (1)

Clemens
Clemens

Reputation: 1817

I believe you need to use an if-then-else flow for this:

{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"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/details"
      },
      "population": {
        "$ref": "#/definitions/population"
      },
      "region": {
        "enum": [ "asia", "europe", "australia", "some-pencil-region", "some-oil-pastels-region" ]
      }
    }
  }
 }
},
"definitions": {
  "details": {
  "type": "object",
  "properties": {
    "brand": {
      "type": "string"
    },
    "year": {
      "type": "integer"
    }
  }
},
"population": {
  "type": "object",
    "properties": {
      "change": {
        "type": "integer"
       },
       "year": {
        "type": "integer"
       }
      }
    }
  }
}

Upvotes: 1

Related Questions