0

So I am working on this problem where I have to validate the json schema based on some conditions. If xy-ac is in the schema then I want source to be present there as well. It is required. Currenlty, I am doing this in code by checking if source not in the json payload then throw an exception. I want that validation to be done at json validation schema level.

SOME_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "required": ["a", "b", "c", "datasetInfo", "d"],
    "properties": {
        "source": {"$ref": "#/definitions/source"},
    },
    "additionalProperties": False,
    "definitions": {
        ...
        "subscriptionID": {"type": "string",
                           "pattern": "^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$"},
        "source": {
            "type": "object",
            "required": ["subscriptionIDs"],
            "properties": {
                "subscriptionIDs": {
                    "type": "array",
                    "items": {"$ref": "#/definitions/subscriptionID"},
                    "minItems": 1
                }
            },
            "additionalProperties": False
        },
        "datasetInfo": {
            "type": "object",
            "properties": {
                "xyz-ad": {
                    "type": "object"
                },
                "xy-ac": {
                    "type": "object"
                }
            },
            "additionalProperties": False,
            "minProperties": 1,
        }
    }
}

I have tried a couple of options but am not able to get it to work.

Attempt 1:

 "dependencies": {
      "xy-ac": [{"$ref": "#/definitions/source"}]
  }

Attempt 2:

SOME_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "required": ["a", "b", "c", "datasetInfo", "d"],
    "properties": {
        "source": {"$ref": "#/definitions/source"},
    },
    "additionalProperties": False,
    "definitions": {
        ...
        "subscriptionID": {"type": "string",
                           "pattern": "^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$"},
        "source": {
            "type": "object",
            "required": ["subscriptionIDs"],
            "properties": {
                "subscriptionIDs": {
                    "type": "array",
                    "items": {"$ref": "#/definitions/subscriptionID"},
                    "minItems": 1
                }
            },
            "additionalProperties": False
        },
        "datasetInfo": {
            "type": "object",
            "properties": {
                "xyz-ad": {
                    "type": "object"
                },
                "xy-ac": {
                    "type": "object"
                    "required": ["source"],
                    "properties": {
                    "source": {"$ref": "#/definitions/source"}
                   }
                }
            },
            "additionalProperties": False,
            "minProperties": 1,
        }
    }
}

None of the above approaches worked. I can provide more info if my question is not clear. Appreciate any pointers

1 Answers1

0

Looks like you have a few problems here. Not uncommon, so don't worry.

You tried to use dependentSchemas with draft-07 JSON Schema.

Previously to Draft 2019-09, dependentRequired and dependentSchemas were one keyword called dependencies. If the dependency value was an array, it would behave like dependentRequired and if the dependency value was a schema, it would behave like dependentSchema.

Official docs: https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentrequired


... "required": {"$ref": "#/definitions/source"}

The required keyword takes an array of strings. Most validators would pick up your schema section as an error.

I can't really tell what your second attempt entales. It's best if you provide the FULL Schema.

You might find the docs on conditional validation helpful, specifically how to use if/then/else: https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Yep, I saw that yesterday that draft-07 uses `dependencies` keyword instead of `dependentRequired` or `dependentSchemas`. I have updated my attempts. I thought of giving `if then` a try but doesn't that require the if statement to be an enum of sorts. How would I use it with a property? – GlobeTrotter Oct 03 '22 at 15:48
  • The value of `if` is a subschema. So you can do anything you would in a normal schema. – Relequestual Oct 03 '22 at 15:49
  • Would it be like this? `"if": { "xy-ac"}, "then": {{"$ref": "#/definitions/source"}}.` Forgive my noobness. First time dealing with a more complex json schema. – GlobeTrotter Oct 03 '22 at 15:55
  • Ok so i tried this now and still seem see some issues with the syntax `"if": { "properties": {"xy-ac"} }, "then": { "properties": {{"$ref": "#/definitions/source"}} }` – GlobeTrotter Oct 03 '22 at 17:28