JSON模式中如何执行数组验证?
我有一个JSON模式片段JSON模式中如何执行数组验证?,json,jsonschema,Json,Jsonschema,我有一个JSON模式片段 "attributes": { "type": "array", "minItems": 3, "items": { "type": "string", "enum": [ "a", "b",
"attributes": {
"type": "array",
"minItems": 3,
"items": {
"type": "string",
"enum": [
"a",
"b",
"c"
]
}
}
我想允许所有的文档,在属性数组中至少有3个元素。它们的值必须是a、b和c,但除此之外,我不想拒绝可能扩展该列表的文档。
例如,我希望以下代码段有效:
"attributes": ["x", "a", "b", "z", "c"]
当前我的验证失败,因为数组中有其他值
请告知。对于草稿-06,您可以使用包含以下内容的关键字: 对于draft-04,您需要使用:
{
"type": "array",
"minItems": 3,
"allOf": [
{ "not": { "items": { "not": { "enum": ["a"] } } } },
{ "not": { "items": { "not": { "enum": ["b"] } } } },
{ "not": { "items": { "not": { "enum": ["c"] } } } }
]
}
我找到了解决问题的方法:
"attributes": {
"type": "array",
"uniqueItems": true,
"minItems": 3,
"items": [
{"type": "string", "enum": ["a"]},
{"type": "string", "enum": ["b"]},
{"type": "string", "enum": ["c"]}
]
}
但我不喜欢这种方法,因为我必须按照模式中所需元素的顺序指定它们。
例如:属性:[a,b,c,z,x]可以正常工作。但如果我搞乱了所需属性的顺序,验证就会失败。
属性:[b、a、c、z、x]-架构无效。
把它修好也很好 我在草稿-04上。它确实有效!看起来有点黑,但它解决了我的问题。非常感谢您的帮助@esp您可以发这个粗俗的双重否定,因为并非所有项目都与“a”不同,因为至少有一个项目是“a”
"attributes": {
"type": "array",
"uniqueItems": true,
"minItems": 3,
"items": [
{"type": "string", "enum": ["a"]},
{"type": "string", "enum": ["b"]},
{"type": "string", "enum": ["c"]}
]
}