I'm not sure if this is possible with JSON schema, but I have data similar to this:
[1, 1, [0, 0, [true]], true]
How can I validate [0, 0, 1] so that at least one of the items is 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. I misunderstood, that anyOf, allOf and oneOf are reserved for that...
What you need is a contains
keyword. This is planned to be added in the next version of the JSON Schema specification. Until that is implemented, you can do it without contains
, but the logic is a bit complicated. 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] }
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.