Use conditional statements on json schema based on another schema object

I think you're having a similar problem as in this question.

@Relequestual is right in that you need the properties keyword around your SDK callout. But for what you want to do, you need to reorganize.

Subschemas only operate on their level in the instance, not at the root.

Consider this schema for a simple JSON object instance containing a one and a two property:

{
  "properties": {
    "one": {
      "enum": ["yes", "no", "maybe"]
    },
    "two": {
      "if": {
        "properties": {
          "one": {"const": "yes"}
        }
      },
      "then": {
        ...       // do some assertions on the two property here
      },
      "else": {
        ...
      }
    }
  }
}

The if keyword under the two property can only consider the portion of the instance under the two property (i.e. two's value). It's not looking at the root of the instance, so it can't see the one property at all.

To make it so that the subschema under the two property subschema can see the one property in the instance, you have to move the if outside of the properties keyword.

{
  "if": {
    "properties": {
      "one": {"const" : "yes"}
    }
  },
  "then": {
    ...       // do some assertions on the two property here
  },
  "else": {
    ...       // assert two here, or have another if/then/else structure to test the one property some more
  }
}

For two possible values of one, this is pretty good. Even three possible values isn't bad. However, as the possible values of one increases, so does the nesting of ifs, which can make your schema horrible to read (and possibly make validation slower).

Instead of using the if/then/else construct, I suggest using an anyOf or oneOf where each subschema represents a valid state for the instance, given the varying values of one.

{
  "oneOf": [
    {
      "properties": {
        "one": {"const": "yes"},
        "two": ...         // do some assertions on the two property here
      }
    },
    {
      "properties": {
        "one": {"const": "no"},
        "two": ...         // do some assertions on the two property here
      }
    },
    {
      "properties": {
        "one": {"const": "maybe"},
        "two": ...         // do some assertions on the two property here
      }
    }
  ]
}

This is much cleaner in my opinion.

Hopefully that explanation helps you reconstruct your schema to allow those other instances to pass.


You have to move your conditional to a high enough level to be able to reference all of the the properties it needs to reference. In this case, that's the /definitions/Base schema. Then you just need to write your schemas properly as Relequestual explained.

{
  "$ref": "#/definitions/Base",
  "definitions": {
    "Base": {
      "type": "object",
      "properties": {
        "session": { "$ref": "#/definitions/Session" },
        "sdk": { "$ref": "#/definitions/SDK" }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "sdk": {
                "properties": {
                  "name": { "const": "ios" }
                }
              }
            },
            "required": ["sdk"]
          },
          "then": {
            "properties": {
              "session": {
                "properties": {
                  "session_id": { "pattern": "A" }
                }
              }
            }
          },
          "else": {
            "properties": {
              "session": {
                "properties": {
                  "session_id": { "pattern": "B" }
                }
              }
            }
          }
        }
      ]
    },
  ...
}