2

My question for jsonschema is twofold:

Given

{
  "foo": {"ar": {"a": "r"}},
  "bar": ""
}
  1. How do I check if the key "ar" exists inside of "foo"?

  2. And only if "ar" exists inside of "foo", how do I make it so that "bar" must exists inside the given json?

I have tried looking other SO answers or jsonschema docs, but they only seem to check if the key has a specific value rather than if the key just exists regardless of its value. And the jsonschema for nested objects only seem to check for the deepest level of the nest rather than somewhere in the middle.

I have come up with this, but it doesn't work.

{
  "definitions": {},
  "$schema": "https://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/root.json",
  "type": "object",
  "properties": {
    "foo": {
      "type": "object"
    },
    "bar": {
      "type": "string"
    }
  },
  "required": [
    "foo"
  ],
  "if": {
    "properties": {
      "foo": {
        "properties": {
          "ar": {
            "type": "object"
          }
        }
      }
    }
  },
  "then": {
    "required": [
      "bar"
    ]
  }
}
Flair
  • 2,609
  • 1
  • 29
  • 41

2 Answers2

2

To test if the property is present, use the required keyword.

{
  "properties": {
    "foo": {
      "required": ["ar"]
    }
  },
  "required": ["foo"]
}

This schema validates to true if /foo/ar is present and false if it's not. Use this in place of your if schema and your conditional should work as expected.

Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
0

Question is answered, but I believe that my case and example may be useful to someone So here I'm looking at "raceHour" field, and if it's presented, then "racingData" field should be presented in "event" and "participants"

And if "raceHour" is not presented, then "racingData" field should not be presented in "event" and "participants"

Json schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "event": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string"
                },
                "raceHour": {
                    "type": "string"
                },
                "racingData": {
                    "type": "object",
                    "properties": {
                        "spotlight": {
                            "type": "string"
                        }
                    }
                },
                "participants": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "racingData": {
                                "type": "object",
                                "properties": {
                                    "spotlight": {
                                        "type": "string"
                                    }
                                }
                            }
                        },
                        "additionalProperties": true
                    },
                    "additionalItems": false
                }
            },
            "if": {
                "not": {
                    "properties": {
                        "raceHour": {
                            "type": "null"
                        }
                    }
                }
            },
            "then": {
                "required": ["racingData"],
                "properties": {
                    "participants": {
                        "items": {
                            "required": [
                                "racingData"
                            ]
                        }
                    }
                }
            },
            "else": {
                "not": {
                    "required": ["racingData"]
                },
                "properties": {
                    "participants": {
                        "items": {
                            "not": {
                                "required": [
                                    "racingData"
                                ]
                            }
                        }
                    }
                }

            },
            "additionalProperties": true,
            "required": [
                "type"
            ]
        }
    },
    "required": [
        "event"
    ]
}

Json (valid one, you need to mess with "raceHour" and "racingData" fields to test the validation logic):

{
    "event": {
        "id": "SBTE_28704119",
        "type": "HeadToHead",
        "raceHour": "1",
        "racingData": {},
        "participants": [
            {
                "id": "SBTP_1906251",
                "name": "Tenshin Nasukawa",
                "venueRole": "Home",
                "racingData": {}
            },
            {
                "id": "SBTP_1906252",
                "name": "Yuki Yonaha",
                "venueRole": "Away",
                "racingData": {}
            }
        ]
    }
}
Wonderio619
  • 135
  • 1
  • 3
  • 11