例如,有2个json属性X和Y,每个属性都是可选的,但其中一个必须是X或Y.如何定义这样的json模式?
答案 0 :(得分:0)
您可以使用oneOf指定所需属性的可能组合。另请参阅How to make json-schema to allow one but not another field?
"oneOf": [
{
"properties": {
"A": {"$ref": "aType"},
"B": {"$ref": "bType"},
"X": {"$ref": "xType"}
}
"required": ["A", "B", "X" ],
"additionalProperties": false
},
{
"properties": {
"A": {"$ref": "aType"},
"B": {"$ref": "bType"},
"Y": {"$ref": "yType"}
}
"required": ["A", "B", "Y" ],
"additionalProperties": false
}
]