如何使用 jsonschema 验证 JSON,在对象数组中,每个对象中的特定键必须是唯一的?例如,验证每个名称 k-v 对的唯一性应该失败:
"test_array": [
{
"Name": "name1",
"Description": "unique_desc_1"
},
{
"Name": "name1",
"Description": "unique_desc_2"
}
]
由于描述键是唯一的,因此在 test_array 上使用 uniqueItems 将不起作用。
我找到了使用允许任意属性的模式的替代方法。唯一需要注意的是 JSON 允许重复的对象键,但重复项将覆盖其先前的实例。具有“Name”键的对象数组可以转换为具有任意属性的对象:
例如,以下 JSON:
"test_object": {
"name1": {
"Desc": "Description 1"
},
"name2": {
"Desc": "Description 2"
}
}
将具有以下架构:
{
"type": "object",
"properties": {
"test_object": {
"type": "object",
"patternProperties": {
"^.*$": {
"type": "object",
"properties": {
"Desc": {"type" : "string"}
},
"required": ["Desc"]
}
},
"minProperties": 1,
"additionalProperties": false
}
},
"required": ["test_object"]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)