在 oneOf 中使用多个 anyOf
Using multiple anyOf inside oneOf
我想创建一个架构,其中我将在 "oneOf" 中包含多个对象,其中包含许多 anyOf 格式的对象,其中一些键可以是所需类型(这部分有效)
我的架构:-
{
"description": "schema v6",
"type": "object",
"oneOf": [
{
"properties": {
"Speed": {
"items": {
"anyOf": [
{
"$ref": "#/definitions/speed"
},
{
"$ref": "#/definitions/SituationType"
}
]
},
"required": [
"speed"
]
}
},
"additionalProperties": false
}
],
"definitions": {
"speed": {
"description": "Speed",
"type": "integer"
},
"SituationType": {
"type": "string",
"description": "Situation Type",
"enum": [
"Advice",
"Depend"
]
}
}
}
但是当我尝试验证此架构时,我能够验证一些不正确的值,例如
{
"Speed": {
"speed": "ABC",//required
"SituationType1": "Advisory1" //optional but key needs to be correct
}
}
我期待的正确回答是
{
"Speed": {
"speed": "1",
"SituationType": "Advise"
}
}
首先,您需要正确设置架构类型,否则实施可能会假设您使用的是最新的 JSON 架构版本(当前为 draft-7)。
因此,在您的架构根目录中,您需要以下内容:
"$schema": "http://json-schema.org/draft-06/schema#",
其次,items
仅适用于目标是数组的情况。
目前您的架构仅检查以下内容:
If the root object has a property of "Speed", it must have a key of
"speed". The root object must not have any other properties.
仅此而已。
您对 definitions
的使用以及您引用它们的方式可能不是您想要的。
看起来你希望 Speed
包含 speed
,它必须是一个整数,并且可选 SituationType
必须是一个字符串,受枚举限制,没有别的。
这是我基于此的架构,它根据您给定的示例数据正确通过和失败:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"oneOf": [
{
"properties": {
"Speed": {
"properties":{
"speed": {
"$ref": "#/definitions/speed"
},
"SituationType": {
"$ref": "#/definitions/SituationType"
}
},
"required": [
"speed"
],
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
"speed": {
"description": "Speed",
"type": "integer"
},
"SituationType": {
"type": "string",
"description": "Situation Type",
"enum": [
"Advice",
"Depend"
]
}
}
}
您需要为 Speed
定义属性,否则您无法阻止其他属性,因为 additionalProperties
仅受相邻的 properties
键的影响。我们希望在 draft-8 中创建一个新关键字来支持这种行为,但在您的示例中您似乎不需要它 (Huge Github issue in relation)。
将 additionalProperties
false 添加到 Speed
架构现在会阻止 object.
中的其他键
我怀疑鉴于您的问题标题,这里可能有更多模式在起作用,并且您已针对此问题对其进行了简化。如果您有更详细的架构和更复杂的问题,我也很乐意提供帮助。
我想创建一个架构,其中我将在 "oneOf" 中包含多个对象,其中包含许多 anyOf 格式的对象,其中一些键可以是所需类型(这部分有效) 我的架构:-
{
"description": "schema v6",
"type": "object",
"oneOf": [
{
"properties": {
"Speed": {
"items": {
"anyOf": [
{
"$ref": "#/definitions/speed"
},
{
"$ref": "#/definitions/SituationType"
}
]
},
"required": [
"speed"
]
}
},
"additionalProperties": false
}
],
"definitions": {
"speed": {
"description": "Speed",
"type": "integer"
},
"SituationType": {
"type": "string",
"description": "Situation Type",
"enum": [
"Advice",
"Depend"
]
}
}
}
但是当我尝试验证此架构时,我能够验证一些不正确的值,例如
{
"Speed": {
"speed": "ABC",//required
"SituationType1": "Advisory1" //optional but key needs to be correct
}
}
我期待的正确回答是
{
"Speed": {
"speed": "1",
"SituationType": "Advise"
}
}
首先,您需要正确设置架构类型,否则实施可能会假设您使用的是最新的 JSON 架构版本(当前为 draft-7)。
因此,在您的架构根目录中,您需要以下内容:
"$schema": "http://json-schema.org/draft-06/schema#",
其次,items
仅适用于目标是数组的情况。
目前您的架构仅检查以下内容:
If the root object has a property of "Speed", it must have a key of "speed". The root object must not have any other properties.
仅此而已。
您对 definitions
的使用以及您引用它们的方式可能不是您想要的。
看起来你希望 Speed
包含 speed
,它必须是一个整数,并且可选 SituationType
必须是一个字符串,受枚举限制,没有别的。
这是我基于此的架构,它根据您给定的示例数据正确通过和失败:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"oneOf": [
{
"properties": {
"Speed": {
"properties":{
"speed": {
"$ref": "#/definitions/speed"
},
"SituationType": {
"$ref": "#/definitions/SituationType"
}
},
"required": [
"speed"
],
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
"speed": {
"description": "Speed",
"type": "integer"
},
"SituationType": {
"type": "string",
"description": "Situation Type",
"enum": [
"Advice",
"Depend"
]
}
}
}
您需要为 Speed
定义属性,否则您无法阻止其他属性,因为 additionalProperties
仅受相邻的 properties
键的影响。我们希望在 draft-8 中创建一个新关键字来支持这种行为,但在您的示例中您似乎不需要它 (Huge Github issue in relation)。
将 additionalProperties
false 添加到 Speed
架构现在会阻止 object.
我怀疑鉴于您的问题标题,这里可能有更多模式在起作用,并且您已针对此问题对其进行了简化。如果您有更详细的架构和更复杂的问题,我也很乐意提供帮助。