【发布时间】:2019-08-20 18:16:07
【问题描述】:
我似乎找不到在枚举上应用多个 if/then 逻辑的工作方式。
anyOf 不应用条件逻辑,而是说如果其中任何一个匹配,那就很好。
allOf 再次不应用条件逻辑,而是测试属性/必填字段的超集。
这是一个 JSON 模式示例:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"type"
],
"properties": {
"type": {
"$id": "#/properties/type",
"enum": [
"a",
"b",
"c"
],
"title": "The Type"
},
"options": {
"$id": "#/properties/options",
"type": "object",
"title": "The Options Schema",
"oneOf": [
{
"if": { "properties": { "type": { "const": "a" } }
},
"then": {
"required": [ "option1" ],
"properties": {
"option1": {
"$id": "#/properties/options/properties/option1",
"type": "boolean",
"title": "The option1 Schema"
}
}
}
},
{
"if": { "properties": { "type": { "const": "b" } }
},
"then": {
"required": [ "option2" ],
"properties": {
"option2": {
"$id": "#/properties/options/properties/option2",
"type": "boolean",
"title": "The option2 Schema"
}
}
}
},
{
"if": { "properties": { "type": { "const": "c" } }
},
"then": {
"required": [],
"properties": {}
}
}
]
}
}
}
如果您针对此 JSON 进行验证:
{
"type": "a",
"options": {
"option1": true
}
}
它失败了,因为option2 是必需的。
如果你改成anyOf就成功了,但是如果你把JSON改成无效:
{
"type": "a",
"options": {
"option2": false
}
}
还是成功了。
我也没有设法让嵌套 if/then/else/if/then/else 工作。
如何检查每个type 的属性集,而您不能将它们混合在一起?这真的可能吗,还是我应该改变我的设计。
【问题讨论】:
标签: json schema jsonschema