JSON Schema Validator passes despite issues

54 Views Asked by At

my goal is to create a schema containing any number array of items where each individual item has "key" string property based on which the "data" property schema changes.

Below is the schema I use to validate and the JSON I expect the validation to fail. As you can see the given example contains properties reserved for different condition, so I expected it to fail.

Any ideas how can I improve the schema so it validates properly?

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "components": {
            "type": "array",
            "minItems": 1,
            "items": {
                "type": "object",
                "anyOf": [
                  {
                    "if": {
                        "required": ["key"],
                        "properties": {
                          "key": { "const": "x" }
                        }
                    },
                    "then": {
                       "required": ["prop1"],
                       "properties": {
                         "prop1": {
                            "type": "string"
                         }
                       }
                    }
                  },
                  {
                    "if": {
                        "required": ["key"],
                        "properties": {
                          "key": { "const": "y" }
                        }
                    },
                    "then": {
                       "required": ["prop2"],
                       "properties": {
                         "prop2": {
                            "type": "string"
                         }
                       }
                    }
                  }
                    
                ]
            }
        }
    },
    "required": [
        "components"
    ],
}
{ components: [
  {
    key: "x",
    data: {
        prop2: 'prop'
    }
  },
    {
    key: "y",
    data: {
        prop1: 'prop'
    }
  }
]
}
1

There are 1 best solutions below

1
arslion On

This might help:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "components": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "oneOf": [
              {
                "if": {
                  "properties": {
                    "key": {
                      "const": "x"
                    }
                  },
                  "required": ["key"]
                },
                "then": {
                  "properties": {
                    "data": {
                      "required": ["prop1"],
                      "properties": {
                        "prop1": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "key": {
                      "const": "y"
                    }
                  },
                  "required": ["key"]
                },
                "then": {
                  "properties": {
                    "data": {
                      "required": ["prop2"],
                      "properties": {
                        "prop2": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        "required": ["key", "data"]
      }
    }
  },
  "required": [
    "components"
  ]
}