问题描述
我无法想出一个 JSON 模式来验证 JSON 是否包含其中之一:
I am having trouble coming up with a JSON schema that will validate if the JSON contains either:
- 只有一个字段
- 仅另一个字段
- (其他两个字段之一)仅
但当存在多个时不匹配.
but not to match when multiples of those are present.
就我而言,我想要一个
copyAll
文件名
matchesFiles
和/或doesntMatchFiles
验证,但我不想接受更多.
to validate but I don't want to accept when more than that is there.
这是我到目前为止所得到的:
Here's what I've got so far:
{
"$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"]}}]}
]
} ;
这比我想要的更匹配.我希望它与以下所有内容相匹配:
This matches more than I want to. I want this to match all of the following:
{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": ["aab", "cab"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "unrelatedA":"xxx"}
{"doesntMatchFiles": ["a*"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "doesntMatchFiles": ["*b"], "unrelatedA":"xxx"}
但不匹配:
{"copyAll": true, "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"copyAll": true, "doesntMatchFiles": ["*b"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"unrelatedA":"xxx"}
我猜我遗漏了一些明显的东西 - 我想知道它是什么.
I'm guessing there's something obvious I'm missing - I'd like to know what it is.
推荐答案
问题在于非"语义.不需要"并不意味着禁止包含".这只是意味着您不必添加它来验证该架构.
The problem is the "not" semantics. "not required" does not mean "inclusion forbidden". It just means that you don't have to add it in order to validate that schema.
但是,您可以使用oneOf"以更简单的方式满足您的规范.请记住,这意味着只有这些模式中的一个可以验证".以下架构实现了您试图解决的属性切换:
However, you can use "oneOf" to satisfy your specification in a simpler way. Remember that it means that "just one of these schemas can validate". The following schema achieves the property switching you are attempting to solve:
{
"$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"
]
},
{
"required": [
"fileNames"
]
},
{
"anyOf": [
{
"required": [
"matchesFiles"
]
},
{
"required": [
"doesntMatchFiles"
]
}
]
}
]
}
这篇关于我如何需要一个字段或另一个字段或(其他两个字段之一)但不是所有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!