How to remove allOf from json schema based on a json value

165 views Asked by At

The following json schema works the following way (DEMO):

  • If switch is true, objectDependingOnSwitch will have a string property called switchIsTrue.
  • If switch is false, objectDependingOnSwitch will have a string property called switchIsFalse.
{
  "type": "object",
  "properties": {
    "switch": {
      "type": "boolean"
    },
    "objectDependingOnSwitch": {
      "type": "object"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "switch": {
            "const": true
          }
        }
      },
      "then": {
        "properties": {
          "objectDependingOnSwitch": {
            "type": "object",
            "properties": {
              "switchIsTrue": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "switch": {
            "const": false
          }
        }
      },
      "then": {
        "properties": {
          "objectDependingOnSwitch": {
            "type": "object",
            "properties": {
              "switchIsFalse": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  ]
}

Is there any way to remove the allOf property based on a given json value?

For example, for following json value:

{
  switch: true
}

I want the discriminatory json schema:

{
  "type": "object",
  "properties": {
    "switch": {
      "type": "boolean"
    },
    "objectDependingOnSwitch": {
      "type": "object",
      "properties": {
        "switchIsTrue": {
          "type": "string"
        }
      }
    }
  }
}
0

There are 0 answers