我在设置JSON模式时遇到问题,该模式将验证JSON是否包含:
但是当它们的倍数存在时不匹配.
在我的具体情况下,我想要一个
copyAllfileNamesmatchesFiles 和/或 doesntMatchFiles验证,但我不想接受超过那个.
这是我到目前为止所得到的:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [ "unrelatedA" ],
"properties": {
"unrelatedA": {
"type": "string"
},
"fileNames": {
"type": "array"
},
"copyAll": {
"type": "boolean"
},
"matchesFiles": {
"type": "array"
},
"doesntMatchFiles": {
"type": "array"
}
},
"oneOf": [
{"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
{"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
{"anyOf": [
{"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
{"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
]
} ;
Run Code Online (Sandbox Code Playgroud)
这比我想要的更多.我希望这匹配以下所有内容:
{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": …Run Code Online (Sandbox Code Playgroud) 在我的模式中,我声明了这些属性:
"index_name": {
"type": "string",
"examples": ["foo-wwen-live", "foo"]
},
"locale": {
"type": "string",
"examples": ["wwen", "usen", "frfr"]
},
"environment": {
"type": "string",
"default": "live",
"examples": [
"staging",
"edgengram",
"test"
]
}
Run Code Online (Sandbox Code Playgroud)
我希望根据我的模式验证的 JSON 主体仅在以下情况下才有效:
index_name 是存在,并且两者 locale和environment 不存在;locale和/或enviroment 是存在,并且index_name 不存在总之,locale而environment绝不应该以混合index_name。
这些应该通过:
案例#1
{
"locale": "usen"
}
Run Code Online (Sandbox Code Playgroud)
案例#2
{
"environment": "foo"
}
Run Code Online (Sandbox Code Playgroud)
案例#3
{
"environment": "foo",
"locale": "usen"
}
Run Code Online (Sandbox Code Playgroud)
案例#4 …
我正在尝试实现这种条件:如果存在一个特定的属性,则需要另一个属性;但如果不存在,则不需要另一个。
另外,在JSON模式中,可以不使用依赖项吗?
这是一个示例架构
var schema = {
"properties": {
"smaller": {
"type": "number"
},
"larger": { "type": "number" },
"medium":{'type':'string'},
"bulky":{'type':'string'}
},
require:['smaller','larger'],
additionalProperties:false
};
Run Code Online (Sandbox Code Playgroud)
如果存在“中等”,则需要“庞大”。否则,不需要“庞大”。
此处的“不需要”是指如果“中”不存在,则必须不存在大体积。
我想对我的架构执行 JSON 验证,该架构有四个属性:
grouppartitionselectfeatures如果使用 或group,partition则features需要。如果使用select,则被features禁止。(请注意,这似乎与这个问题中的情况不同- 我不想将其设置为features“不需要”,但如果包含它,则会出现验证错误。
所以我的三个选择是:
group和featurespartition和featuresselect并不是features我将它们编码为:
"oneOf": [
{ "required": [ "group", "features" ] },
{ "required": [ "partition", "features" ] },
{ "required": [ "select" ] }
]
Run Code Online (Sandbox Code Playgroud)
但我不知道如何适当地强制features排除在最后一个选项之外 - 或者这是否有可能?