JSON模式中用于布尔项列表的XOR逻辑 [英] XOR logic in JSON schema for a list of boolean items
问题描述
我在验证布尔值列表时遇到问题.我的输入是:
I faced a problem with validating a list of boolean values. My input is:
[true,true,false]
,并且它应该不进行验证,因为只有具有一个和唯一一个真值的列表才应该为真.目前,我的架构通过接受一个或多个真实值(但不是全部)来进行某种包容性OR:
and it should not verify this because only lists with one and only one true value, should be true. At the moment my schema does a sort of inclusive OR by accepting one or more true values, but not all:
{
"type": "array",
"items": {
"$ref": "#/definitions/_items"
},
"$ref": "#/definitions/xor",
"definitions": {
"xor": {
"oneOf": [
{
"$ref": "#/definitions/or"
},
{
"$ref": "#/definitions/and"
}
]
},
"_items": {
"enum": [
true,
1
]
},
"or": {
"not": {
"type": "array",
"items": {
"not": {
"$ref": "#/definitions/_items",
"maximum": 1,
"minimum": 1
}
}
}
},
"and": {
"type": "array",
"items": {
"$ref": "#/definitions/_items"
}
}
}
}
如您所见,我尝试使用最大和最小属性来解决它,但是它们似乎并不影响结果.
As you can see, I have tried to solve it with maximum and minimum attributes but they don't seem to affect the outcome.
一个真值表将正确地是:
One truth table would correctly be:
[true,true,false]
=>假
[true,true,true]
=>假
[true,false,false]
=>是
[false,false,false]
=>假
[false,true,false]
=>是
[false,false,true]
=>是
[true,false,true]
=>假
[false,true,true]
=>假
推荐答案
您可以显式使用真值表:
You can explicitly use your truth table:
{
"type": "array",
"minItems": 3,
"maxItems": 3,
"anyOf": [
{
"items": [
{ "enum": [true] },
{ "enum": [false] },
{ "enum": [false] }
]
},
{
"items": [
{ "enum": [false] },
{ "enum": [true] },
{ "enum": [false] }
]
},
{
"items": [
{ "enum": [false] },
{ "enum": [false] },
{ "enum": [true] }
]
}
]
}
如果您想要一个包含三个以上项目的通用解决方案,那么它很快就会失去控制.
If you want a general solution with more than 3 items it quickly gets out of hand.
草稿-06定义了关键字包含",它可以验证至少一个项目与某个模式匹配(但不完全匹配一个模式),但是据我所知,标准的JSON模式关键字不允许您想要的内容.
draft-06 defines keyword "contains" that allows to validate that at least one items matches some schema (but not exactly one), but as far as I know the standard JSON-schema keywords don't allow what you want.
您可以通过编程方式生成模式(针对任何固定数量的项目),也可以在不使用模式的情况下进行验证.
You can either generate schema programmatically (for any fixed number of items) or validate without schema.
这篇关于JSON模式中用于布尔项列表的XOR逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!