Json 具有复杂条件的 json 模式的模式
Json Schema for a json schema with complex conditions
我需要验证给定的 JSON 架构实例是否遵循某些规则。
大多数规则都可以根据其他模式进行检查,但我无法使用模式检查 2 个条件:
- 对于每个 'object' 类型,'properties' 中的每个 属性 也必须在 'required' 列表中。
- '$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?
不,JSON 架构不可能做到这一点。您不能引用实例数据。
- for each 'object' type, each property in 'properties' must be also in the 'required' list.
无法完成,因为...
- Is it possible to refer to the current instance being validated(similarly that '$ref':"#" is referring to the current schema)
没有。曾有人提议使用一个名为 $data
的关键字,但该概念存在太多问题,因此未能实现。
- Is it possible somehow using the 'dependencies' validator somehow to validate condition 1?
没有
- '$schema' must be and can only be in the root of the schema.
这一项你可以做到。基本上,您创建包含所有定义的“模式”定义。然后你可以扩展“架构”以在根架构中要求 $schema
或在 sub-schemas.
中禁止 $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"] }
}
}
}
我需要验证给定的 JSON 架构实例是否遵循某些规则。 大多数规则都可以根据其他模式进行检查,但我无法使用模式检查 2 个条件:
- 对于每个 'object' 类型,'properties' 中的每个 属性 也必须在 'required' 列表中。
- '$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?
不,JSON 架构不可能做到这一点。您不能引用实例数据。
- for each 'object' type, each property in 'properties' must be also in the 'required' list.
无法完成,因为...
- Is it possible to refer to the current instance being validated(similarly that '$ref':"#" is referring to the current schema)
没有。曾有人提议使用一个名为 $data
的关键字,但该概念存在太多问题,因此未能实现。
- Is it possible somehow using the 'dependencies' validator somehow to validate condition 1?
没有
- '$schema' must be and can only be in the root of the schema.
这一项你可以做到。基本上,您创建包含所有定义的“模式”定义。然后你可以扩展“架构”以在根架构中要求 $schema
或在 sub-schemas.
$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"] }
}
}
}