santhosh-tekuri/jsonschema

Trouble with conditionals

Closed this issue · 2 comments

I am having a lot of trouble with using conditionals and I am not sure if it is my schema or a bug.

I have the property actions which has other properties who's keys are arbitrarily user defined.
The behavior I am trying to invoke for objects in actions is:

  • All objects are required to have the field action, meaning if we dont have it validation should fail.
  • If action is present, then I need one of the following:
    • if action = jump, then require property how_high
    • if action = sit, then require property where
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "actions": {
      "type":"object",
      "minProperties": 1,
      "patternProperties": {
        ".*":{
          "type":"object",
          "required": ["action"],
          "properties": {
            "action": {
              "type": "string"
            },
            "description": {
              "type": "string"
            }
          },
          "if":{
            "properties": { "action": {"type": "string"}}
          },
          "then": {
            "oneOf": [
              {
                "if": {
                  "properties": { "action": { "const": "jump" } }
                },
                "then": {
                  "properties": {
                    "how_high": { "type":"string"}
                  },
                  "required": ["how_high"]
                }
              },
              {
                "if": {
                  "properties": { "action": { "const": "sit" } }
                },
                "then": {
                  "required": ["where"],
                  "properties": {
                    "where":{ "type":"string" }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

This is the JSON I am trying to validate:

{
  "actions": {
    "jump_up1": {
      "action": "jump",
      "how_high": "very high"
    },
    "missing_action": {
      "description": "This should fail because we dont have action"
    },
    "sitdown": {
      "action": "sit",
      "where": "the floor"
    }
  }
}

However, this is the result I am getting:

I[#/actions] S[#/properties/actions] validation failed
  I[#/actions/jump_up1] S[#/properties/actions/patternProperties/.%2A/then] if-then failed
    I[#/actions/jump_up1] S[#/properties/actions/patternProperties/.%2A/then/oneOf] valid against schemas at indexes 0 and 1
  I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A] validation failed
    I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then] if-then failed
      I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then/oneOf] oneOf failed
        I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then/oneOf/0/then] if-then failed
          I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then/oneOf/0/then/required] missing properties: "how_high"
        I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then/oneOf/1/then] if-then failed
          I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/then/oneOf/1/then/required] missing properties: "where"
    I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/required] missing properties: "action"
  I[#/actions/sitdown] S[#/properties/actions/patternProperties/.%2A/then] if-then failed
    I[#/actions/sitdown] S[#/properties/actions/patternProperties/.%2A/then/oneOf] valid against schemas at indexes 0 and 1

Taking these one at a time:

  • Since missing_action does not have property action this, then should not be validating it
  • Since jump does not match sit, oneOf should only match index 0
  • Since sit does not match jump, oneOf should only match index 1

This is very baffling.

I have also tried this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "actions": {
      "type":"object",
      "minProperties": 1,
      "patternProperties": {
        ".*":{
          "type":"object",
          "required": ["action"],
          "properties": {
            "action": {
              "type": "string"
            },
            "description": {
              "type": "string"
            }
          },
          "oneOf": [
            {
              "if": {
                "properties": { "action": { "const": "jump" } }
              },
              "then": {
                "properties": {
                  "how_high": { "type":"string"}
                },
                "required": ["how_high"]
              }
            },
            {
              "if": {
                "properties": { "action": { "const": "sit" } }
              },
              "then": {
                "required": ["where"],
                "properties": {
                  "where":{ "type":"string" }
                }
              }
            }
          ]
        }
      }
    }
  }
}

But the error is very similar:

I[#/actions] S[#/properties/actions] validation failed
  I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A] validation failed
    I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/oneOf] oneOf failed
      I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/oneOf/0/then] if-then failed
        I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/oneOf/0/then/required] missing properties: "how_high"
      I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/oneOf/1/then] if-then failed
        I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/oneOf/1/then/required] missing properties: "where"
    I[#/actions/missing_action] S[#/properties/actions/patternProperties/.%2A/required] missing properties: "action"
  I[#/actions/sitdown] S[#/properties/actions/patternProperties/.%2A/oneOf] valid against schemas at indexes 0 and 1
  I[#/actions/jump_up1] S[#/properties/actions/patternProperties/.%2A/oneOf] valid against schemas at indexes 0 and 1

use allOf instead of oneOf in your schema, then it works.