occurred
occurred

Reputation: 508

JSON Schema: How to check every array item that has a specific value has the required properties?

How can I check that for every array item, which has in the property field1the value Value1, the property field2 is required? If field1 has another value than Value1 then only field1 is required.

Here is an example:

{
  "property_abc":[
    {
      "field1":"Value1",
      "field2": "Value2"
    },
    {
      "field1":"Value2"
    },
    {
      "field1":"Value3"
    }
  ]
}

And this is my Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "additionalProperties": false,
  "properties": {
    "property_abc": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "field1": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          },
          "field2": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "property_abc": {
                  "items": {
                    "properties": {
                      "field1": {
                        "const": "Value1"
                      }
                    }
                  }
                }
              }
            },
            "then": {
              "required": [
                "field1",
                "field2"
              ]
            },
            "else": {
              "required": [
                "field1"
              ]
            }
          }
        ]
      }
    },
    "property_xyz": {
      "type": "number"
    }
  },
  "type": "object"
}

The above example is correct.

But the following one will throw an error, because for the first item in property_abc the property field2 is required, but not existing:

{
  "property_abc":[
    {
      "field1":"Value1"
    },
    {
      "field1":"Value2"
    },
    {
      "field1":"Value3"
    }
  ]
}

Upvotes: 1

Views: 988

Answers (1)

Ethan
Ethan

Reputation: 725

the if schema you have is looking in the wrong place - this schema applies to the object inside the array value of property_abc. I've pasted the correction below, and also moved it outside the allOf which serves no purpose here.

you might also have a look at the dependencies keyword, it might be helpful, but would take a bit of refactoring to express the constraints you have.

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "additionalProperties": false,
  "properties": {
    "property_abc": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "field1": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          },
          "field2": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          }
        },
        "if": {
          "properties": {
            "field1": {
              "const": "Value1"
            }
          }
        },
        "then": {
          "required": [
            "field1",
            "field2"
          ]
        },
        "else": {
          "required": [
            "field1"
          ]
        }
      }
    },
    "property_xyz": {
      "type": "number"
    }
  },
  "type": "object"
}

Upvotes: 2

Related Questions