2

我正在尝试学习 json 架构,但有些东西对我不起作用。我正在尝试从http://json-schema.org/understanding-json-schema/reference/conditionals.html#id4dependentSchemas运行示例,但它只是没有验证。

我正在使用这个架构:

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"]
    }
  }
}

而这个json,应该会引发一个错误,因为它缺少密钥billing_address

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

但是当我使用jsonschema.validate(dic_check, schema_check)(使用 python,jsonschema 包版本 4.2.1)时,验证通过没有问题。

我在这里做错了什么?

4

1 回答 1

2

如果您使用的实现至少不支持规范的 draft2019-09,dependentSchemas则不会被识别为关键字。在早期版本(draft7 和之前的版本)中,该关键字被称为dependencies,具有相同的语法(实际上,dependencies被分为两个dependentSchemasdependentRequired)。

详细信息在您链接的页面https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentschemas上进行了描述。

如果你仍然相信你所拥有的应该工作,我建议你打开一个关于实现问题队列的错误报告。

于 2021-11-24T16:29:06.097 回答