4

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?

SaBe
  • 43
  • 4
  • Looks like you’re doing everything right here. Could you provide a small block of code to fully reproduce the issue please? – Relequestual Nov 24 '21 at 09:14

1 Answers1

4

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.

Ether
  • 53,118
  • 13
  • 86
  • 159
  • I tried changing to `dependencies`, but it didn't work. After reinstalling the package once again, it works. Thank you – SaBe Nov 30 '21 at 07:49
  • I had this same problem, not sure if it was a pip/homebrew conflict. `brew reinstall jsonschema` and `brew link --overwrite jsonschema` fixed it – David Jan 16 '22 at 12:07