在给定条件的情况下,我想对同一属性进行不同的验证检查。我应该如何使用if-then
来做到这一点?
我尝试做一个oneOf
,然后根据我拥有的if-then
的类型做不同的operator
。因此,如果我在之间,我希望value
像[[1,2],[4,5]]
,但是如果我在 in 中,我希望value
是{ {1}}或[1,2,3]
我的架构如下:
["a","b","c"]
}
%{
definitions: %{
rule: %{
type: "object",
required: ["attribute", "operator", "value"],
properties: %{
attribute: %{
enum: ["show_shop", "shop_item"]
},
operator: %{
enum: ["in", "between"]
},
},
oneOf: [
%{
if: %{definitions: %{rule: %{properties: %{operator: %{const: "between"}}}}},
then: %{
properties: %{
value: %{
type: "array",
minItems: 1,
items: %{
oneOf: [
%{
type: "array",
items: [%{type: "number"}, %{type: "number"}],
additionalItems: false,
minItems: 2,
maxItems: 2
},
%{
type: "array",
items: [%{type: "null"}, %{type: "number"}],
additionalItems: false,
minItems: 2,
maxItems: 2
},
%{
type: "array",
items: [%{type: "number"}, %{type: "null"}],
additionalItems: false,
minItems: 2,
maxItems: 2
}
]
}
},
additionalProperties: false
}
}
},
%{
if: %{definitions: %{rule: %{properties: %{operator: %{const: "in"}}}}},
then: %{
properties: %{
value: %{
type: "array",
items: %{type: "number"},
minItems: 1,
},
additionalProperties: false
}
}
},
%{
if: %{definitions: %{rule: %{properties: %{operator: %{const: "in"}}}}},
then: %{
properties: %{
value: %{
type: "array",
items: %{type: "string"},
minItems: 1,
},
additionalProperties: false
}
}
}
]
}
},
type: "array",
items: %{"$ref": "#/definitions/rule"},
minItems: 1
-> 应该给出错误,因为之间的运算符应只接受数组数组
[ { "value": [1,2,3,4], "operator": "between", "attribute": "show_shop" } ]
-> 应该没问题
[ { "value": [ [1,3] ], "operator": "between", "attribute": "show_shop" } ]
-> 应该给出错误,因为in中的运算符仅接受整数数组或字符串数组
[ { "value": [ [1,3] ], "operator": "in", "attribute": "show_shop" } ]
-> 应该没问题
无论运算符是什么,只要值是列表的列表或数字的列表或字符串的列表,json都是有效的。就像[ { "value": ["a", "b"], "operator": "in", "attribute": "show_shop" } ]
条件根本没有评估。因此,从我之前的所有示例中,我都没有看到任何错误。 (而且我没有任何架构错误)