Validate property value based on the value of another property in JSON schema

22 Views Asked by At

I'm trying to define a JSON schema that includes validation logic that could validate a property based on the value of another property.

{
    "type": "object",
    "properties": {
        "A": {"type": "string"},
        "B": {"type": "string"},
        "C": {"type": "string"}
    }
}

If the value of A is "apple", then the value of B and C must not be "apple". Essentially, the value of a property must be unique across all properties of the same object.

I understand that I can use if/then/else to validate the mutual exclusiveness of values:

{
  "type": "object",
  "properties": {
    "A": {
      "type": "string"
    },
    "B": {
      "type": "string"
    },
    "C": {
      "type": "string"
    }
  },
  "if": {
    "required": [
      "A"
    ],
    "properties": {
      "A": {
        "const": "apple"
      }
    }
  },
  "then": {
    "required": [
      "B",
      "C"
    ],
    "properties": {
      "B": {
        "not": {
          "const": "apple"
        }
      },
      "C": {
        "not": {
          "const": "apple"
        }
      }
    }
  }
}

However, the problem is the possible value of A will not be known upfront. In my use case, users are allowed to enter whatever value they will.

I appreciate any suggestions.

0

There are 0 best solutions below