【发布时间】:2019-11-19 06:45:23
【问题描述】:
我正在尝试验证我认为的简单 JSON 模式作为我的 Python 应用程序的配置文件,它是标题键/值对的列表,唯一的复杂之处是如果“类型”字段设置为“ AnyValue' 则不需要 value 键。
这是架构的原样:
{
"definitions":
{
'KeyEntry':
{
"properties":
{
'Type': {"type" : "string"},
'Key': {"type": "string"}
},
"required": ['Type', 'Key'],
"anyOf":
[
{
"if":
{
"properties": {'Type': {"const": 'ExactValue'}}
},
"then":
{
"properties":
{
'Value': {"type": "string"}
},
"required": ['Type', 'Key', 'Value'],
"additionalProperties": false
}
},
{
"if":
{
"properties": {'Type': {"const": 'AnyValue'}}
},
"then":
{
"required": ['Type', 'Key'],
"additionalProperties": false
}
}
]
}
},
"type": "object",
"properties":
{
'Keys':
{
"type": "array",
"items": {"$ref": "#/definitions/KeyEntry"}
}
},
"required": ['Keys']
}
大多数验证都有效,除非我添加额外的值,即使我在整个架构中设置了 "additionalProperties": false。
这是一个接受额外值的示例:
{
"Keys": [
{
"Type": "AnyValue",
"Key": "Version",
"Y": "Yes",
"N": "No",
}
]
}
请有人帮忙解释一下我哪里出错了,我应该如何纠正?
【问题讨论】:
-
啊,这是一个经典的绊脚石。我现在无法完全检查解决方案,但是如果你将 anyOf 更改为 allOf,并在 if 块中添加 else: false 会发生什么?
-
即使没有帮助,我会尽快提供帮助。 =]
-
@Relequestual 嗨,只要可以到 allOf 的 anyOf,我就会得到 'JSON 不匹配来自 'allOf' 的所有模式。无效的架构索引:1。验证错误,这是在我添加 else 之前 :(
标签: json jsonschema json-schema-validator