Python 具有复杂条件的Json模式的Json模式
我需要验证给定的JSON模式实例是否遵循某些规则。 大多数规则可以根据其他模式进行检查,但我无法使用模式检查2个条件:Python 具有复杂条件的Json模式的Json模式,python,json,jsonschema,Python,Json,Jsonschema,我需要验证给定的JSON模式实例是否遵循某些规则。 大多数规则可以根据其他模式进行检查,但我无法使用模式检查2个条件: 对于每个“对象”类型,“属性”中的每个属性也必须在“必需”列表中 “$schema”必须且只能位于架构的根目录中 事实上,我已经通过在python脚本中创建成功地验证了这些条件,但我想知道, 是否可以纯粹通过使用JSON模式来检查这些条件 如果你很好奇,你可以看到我正在使用的当前模式,尽管我的问题不需要它: { "$schema": "ht
是否可以纯粹通过使用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"
}
如果你不知道这是否可能,也许你可以帮我回答以下问题之一:
不,这在JSON模式中是不可能的。不能引用实例数据
$data
的关键字,但这个概念存在太多问题,因此它从未实现
$schema
,或禁止子模式中的$schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"allOf": [{ "$ref": "#/definitions/schema" }],
"required": ["$schema"],
"definitions": {
"schema": {
"type": "object",
"properties": {
"$id": { "type": "string", "format": "uri" },
"$schema": { "type": "string", "format": "uri" },
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#/definitions/sub-schema" }
}
},
"additionalProperties": false
},
"sub-schema": {
"allOf": [{ "$ref": "#/definitions/schema" }],
"not": { "required": ["$schema"] }
}
}
}
好的,谢谢,那么只在根目录上要求
$schema
怎么样?实际上可能是可能的。我会有一个剧本,然后再给你回复。谢谢你的详细回答,朋友!