[英]Validating nested list of numbers/booleans with JSON schema
I'm not sure if this is possible with JSON schema, but I have data similar to this: 我不确定JSON模式是否可行,但是我有类似的数据:
[1, 1, [0, 0, [true]], true]
How can I validate [0, 0, 1] so that at least one of the items is 1/true? 如何验证[0,0,1],以便至少一项是1 / true?
So far, I have managed to create schema to this point: 到目前为止,我已经设法创建了架构:
{ "type": "array", "items": { "$ref": "#/definitions/_items" }, "definitions": { "_items": { "anyOf": [ { "enum": [ 0, 1 ], "type": "integer" }, { "enum": [ false, true ], "type": "boolean" }, { "type": "array", "items": { "anyOf": [ { "$ref": "#/definitions/_items" } ] } } ] } } }
Apparently it does validate all accepted values, but it doesn't take on account, if there are all, some, one, or none of the values 1 / true. 显然,它确实验证了所有可接受的值,但是,如果存在值1 / true的全部,部分,一个或不存在,则不考虑在内。 I misunderstood, that anyOf, allOf and oneOf are reserved for that... 我误会了,anyOf,allOf和oneOf是为此保留的...
What you need is a contains
keyword. 您需要一个contains
关键字。 This is planned to be added in the next version of the JSON Schema specification. 计划将其添加到下一版本的JSON Schema规范中。 Until that is implemented, you can do it without contains
, but the logic is a bit complicated. 在实现之前,您可以不contains
来完成此操作,但是逻辑有点复杂。 I've also cleaned up some of the unnecessary bits from what you have so far. 到目前为止,我还清理了一些不必要的内容。
{
"type": "array",
"items": { "$ref": "#/definitions/_items" },
"allOf": [{ "$ref": "#/definitions/contains-1-or-true" }],
"definitions": {
"_items": {
"anyOf": [
{ "enum": [0, 1] },
{ "type": "boolean" },
{ "$ref": "#" }
]
},
"contains-1-or-true": {
"not": {
"type": "array",
"items": {
"not": { "enum": [1, true] }
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.