我有一个模式,其中我有一个数组(在传输对象内),它可能有两个选项(A 或 B)之一。
我已经附加了模式和示例数据(当它应该抛出错误时,它实际上正在得到验证)。
在transmitDataDef->items下,我尝试了“anyOf”、“oneOf”、“type”,即使数据不匹配,它们都不会出错(数据包含未在任何地方定义的optionC)
我正在使用 jsonschema python 库。我也在https://www.jsonschemavalidator.net/中尝试过这个模式 + 数据,结果相同。
架构:
{
"definitions": {
"optionADef": {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"enum": [
"random",
"fixed"
]
},
"startbyte": {
"type": "number"
}
},
"required": [
"startbyte"
],
"additionalProperties": false
},
"optionBSubItemDef": {
"type": "object",
"properties": {
"value": {
"type": "number",
"minimum": 0
}
}
},
"optionBSettingsDef": {
"type": "object",
"properties": {
"sequence": {
"type": "number",
"minimum": 0
}
}
},
"optionBDataDef": {
"type": "object",
"properties": {
"subitem": {
"ref": "#/definitions/optionBSubItemDef"
}
}
},
"optionBDef": {
"type": "object",
"properties": {
"_data": {
"ref": "#/definitions/optionBDataDef"
},
"_settings": {
"$ref": "#/definitions/optionBSettingsDef"
}
},
"required": [
"_data"
],
"additionalProperties": false
},
"transmitDataDef": {
"type": "array",
"minItems": 1,
"maxItems": 1,
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"optionA": {
"ref": "#/definitions/optionADef"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"optionB": {
"ref": "#/definitions/optionBDef"
}
},
"additionalProperties": false
}
]
}
},
"transmitSettingsDef": {
"type": "object",
"properties": {
"length": {
"type": "number",
"minimum": 0,
"maximim": 8
}
}
},
"transmitDef": {
"type": "object",
"properties": {
"_data": {
"ref": "#/definitions/transmitDataDef"
},
"_settings": {
"$ref": "#/definitions/transmitSettingsDef"
}
},
"required": [
"_data"
],
"additionalProperties": false
},
"bundleDef": {
"type": "object",
"properties": {
"transmit": {
"$ref": "#/definitions/transmitDef"
}
},
"oneOf": [
{
"required": [
"transmit"
]
}
],
"additionalProperties": false
}
},
"type": "object",
"properties": {
"name": {
"type": "string"
},
"bundle": {
"$ref": "#/definitions/bundleDef"
}
},
"required": [
"name",
"bundle"
]
}
样本数据:
{
"name": "test1",
"bundle": {
"transmit": {
"_settings": {
"length": 0
},
"_data": [
{
"optionC": {
"_settings": {
"sequence": 150
},
"data1": [
{
"subitem": {
"value": 100
}
}
]
}
}
]
}
}
}
我希望验证能够捕获“optionC”并将其标记为错误。如果我有 optionB 而不是 optionC,我希望它将“data1”标记为无效项目。