您需要在架构级别声明一个规则来定义包含这两个属性的对象。然后您可以使用dependentSchemas
, dependentRequired
, or if
/then
/else
关键字来定义规则,例如“if in property A, then in property B”。
https://json-schema.org/understanding-json-schema/reference/conditionals.html https://json-schema.org/understanding-json-schema/reference/conditionals.html
例如:
{
"type": "object",
"properties": {
"A": {
"type": "object",
"properties":{ "X":{"type":"boolean"}}
},
"B": {
"type": "object",
"properties":{...}
}
},
"if": {
"required": ["A"],
"properties": {
"A": {
"required": ["X"],
"properties": { "X": { "const": true } }
}
}
},
"then": {
"required": ["B"],
"properties": {
"B": {
"required": ["Y"],
"properties": { "Y": { "const": "/A/X is true" } }
}
}
}
}
这就是说“如果属性 A 存在,并且有一个为 true 的子属性 X,那么 B 下一定有一个属性 Y,它是字符串“/A/X is true””。