Search code examples
jsonschemajsonschemajson-schema-validator

Why is the json object valid against this conditional schema?


Here is the json object.

{
  "payment": {
    "account": [
      {
        "type": "ACCOUNT_INFORMATION",
        "identification": "2451114"
      },
      {
        "type": "XXX",
        "identification": "2451114"
      }
    ]

  }
}

And this is the schema.

{
  "if": {
    "properties": {
      "payment": {
        "properties": {
          "account": {
            "items": {
              "properties": {
                "type": {
                  "const": "ACCOUNT_INFORMATION"
                }
              }
            }
          }
        }
      }
    }
  },
  "then": {
    "properties": {
      "payment": {
        "properties": {
          "account": {
            "items": {
              "properties": {
                "identification": {
                  "maxLength": 8,
                  "minLength": 8
                }
              }
            }
          }
        }
      }
    }
  }
}

If remove the second account items as follows, the schema gives error.

{
  "payment": {
    "account": [
      {
        "type": "ACCOUNT_INFORMATION",
        "identification": "2451114"
      }
    ]
  }
}

Is this due to the conditional schema cannot be apply to an embedded array?

Validation used https://www.jsonschemavalidator.net/

The first json object returns no error while the second one returns error with violation of minLength constraint.

Should both return error?


Solution

  • To see what's happening, let's break down the schema to focus on the critical part of the if schema.

    "items": {
      "properties": {
        "type": { "const": "ACCOUNT_INFORMATION" }
      }
    }
    

    Given this schema, the following instance is not valid because not all "type" properties have the value "ACCOUNT_INFORMATION".

    [
      {
        "type": "ACCOUNT_INFORMATION",
        "identification": "2451114"
      },
      {
        "type": "XXX",
        "identification": "2451114"
      }
    ]
    

    And, this following value is valid because all "type" properties have the value "ACCOUNT_INFORMATION".

    [
      {
        "type": "ACCOUNT_INFORMATION",
        "identification": "2451114"
      }
    ]
    

    That difference in validation result is the reason these two values behave differently in your schema. The then schema is applied only when the if schema evaluates to true, which is what you get with the second example and not the first. The then schema is applied on the second example and the minLength constraint causes validation to fail.

    It seems like your conditional only applies to the items schema, so you can solve this by moving your conditional into that object only.

    {
      "properties": {
        "payment": {
          "properties": {
            "account": {
              "items": {
                "if": {
                  "properties": {
                    "type": {
                      "const": "ACCOUNT_INFORMATION"
                    }
                  },
                  "required": ["type"]
                },
                "then": {
                  "properties": {
                    "identification": {
                      "maxLength": 8,
                      "minLength": 8
                    }
                  }
                }
              }
            }
          }
        }
      }
    }