Search code examples
jsonschemapython-jsonschema

jsonschema dependentSchema not validating


I am trying to learn json schema, but something isn't working out for me. I'm trying to run the example from http://json-schema.org/understanding-json-schema/reference/conditionals.html#id4 for dependentSchemas, but it just doesn't validate.

I'm using this schema:

check_schema = {"$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "credit_card": { "type": "number" }
  },
  "required": ["name"],
  "dependentSchemas": {
    "credit_card": {
      "properties": {
        "billing_address": { "type": "string" }
      },
      "required": ["billing_address"]
    }
  }
}

and this json, that should raise an error since it is missing the key billing_address:

check_dict={
  "name": "John Doe",
  "credit_card": 5555555555555555
}

but when I use jsonschema.validate(dic_check, schema_check) (with python, jsonschema package version 4.2.1), the validation passes with no issues.

What am I doing wrong here?


Solution

  • If you are using an implementation that doesn't support at least draft2019-09 of the specification, dependentSchemas won't be recognized as a keyword. In earlier versions (draft7 and before), that keyword was known as dependencies, with the same syntax (actually, dependencies was split into two, dependentSchemas and dependentRequired).

    The details are described on the page you linked, https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentschemas.

    If you still believe that what you have should work, I suggest you open a bug report on the implementation's issue queue.