【发布时间】:2020-09-29 14:20:08
【问题描述】:
我需要验证给定的 JSON 模式实例是否遵循某些规则。 大多数规则都可以根据其他模式进行检查,但我无法使用模式检查 2 个条件:
- 对于每个“对象”类型,“属性”中的每个属性也必须在“必需”列表中。
- '$schema' 必须且只能位于架构的根目录中。
事实上,我已经通过在 python 脚本中创建 custom validators 成功验证了这些条件,但我想知道,
是否可以纯粹使用 JSON 模式来检查这些条件?
如果您感兴趣,您可以看到我正在使用的当前架构,尽管我的问题没有必要:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"types": {
"object": {
"_comment": "object type",
"isType": "object",
"properties": {
"type": {"enum": ["object"]},
"properties": {
"type": "object",
"additionalProperties": {"$ref": "#/definitions/schema"},
},
"additionalProperties": {"enum": [False]},
"required": {"type": "array"},
"description": {"type": "string", "maxLength": 128},
"$id": {"type": "string", "maxLength": 128}
},
"dependencies": {
"properties": ["required"],
"required": ["properties"]
},
"required": ["additionalProperties"],
"allRequired": True, # custom validator for requiring all properties
"additionalProperties": False
},
... # extra types that not really relevant
},
"schema": {
"oneOf": [
{"$ref": "#/definitions/types/object"},
{"$ref": "#/definitions/types/string"},
{"$ref": "#/definitions/types/number"},
{"$ref": "#/definitions/types/array"},
{"$ref": "#/definitions/types/enum"},
{"$ref": "#/definitions/types/anyOf"},
],
},
},
"type": "object",
"$ref": "#/definitions/schema"
}
如果您不知道是否可行,也许您可以帮助我解决以下问题之一:
- 是否可以引用当前正在验证的实例(类似于 '$ref':"#" 引用当前架构)
- 是否有可能以某种方式使用“dependencies”验证器来验证条件 1?
【问题讨论】:
标签: python json jsonschema