Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with conditionals #37

Closed
nerdoftech opened this issue Apr 28, 2020 · 2 comments
Closed

Trouble with conditionals #37

nerdoftech opened this issue Apr 28, 2020 · 2 comments

Comments

@nerdoftech
Copy link

nerdoftech commented Apr 28, 2020

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.

@nerdoftech
Copy link
Author

nerdoftech commented Apr 28, 2020

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

@santhosh-tekuri
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants