json模式中多个有条件要求的字段



我有一个架构,如果枚举与某个值匹配,则该架构具有必需的属性。这很好用。

"oneOf": [
{
"not" : {
"properties": {
"other-val": { "enum" : ["ABC"]}
}
}
},
{ "required": ["ABC-detail"] } 
],

但是,我试图添加第二个字段DEF-detail,基于other-value枚举是否为"0";DEF";。我不知道如何将这些依赖关系链接起来。我试着在一个allOf中放入多个oneOf,但我无法将同一密钥的倍数添加到其中

该模式被称为"言外之意";。通过将它们与allOf组合,可以很容易地声明多个含义。

{
"allOf": [
{
"anyOf": [
{
"not": {
"properties": {
"other-val": { "const": "ABC" },
},
"required": ["other-val"]
}
},
{ "required": ["ABC-detail"] }
]
},
... another implication ...,
... another implication ...
]
}

如果你使用的是draft-07或更高版本,你也有if/then,这只是一个暗示的语法糖。

{
"if": {
"properties": {
"other-val": { "const": "ABC" }
},
"required": ["other-val"]
},
"then": { "required": ["ABC-detail"]
}

这个模式可以使用allOf以与蕴涵相同的方式进行组合。

oneOf中的一组条件有效。但我不认为这将DEF排除在abc-thing之外,例如,但在我的情况下,这是可以的。

"oneOf": [
{
"properties": {"other-val": {"enum": ["ABC"]}},
"required": ["abc-thing"]
},
{
"properties": {"other-val": {"enum": ["DEF"]}},
"required": ["def-thing"]
},  
{
"properties": {"other-val": {"enum": ["GHI"]}},
"required": ["ghi-thing"]
},            
{
"not": {"properties": {"other-val": {"enum": ["ABC", "DEF", "GHI"]}}}
}
],

最新更新