我的输入 json 如下所示,
{
"results": [
{
"name": "A",
"testA": "testAValue"
}
]
}
条件是,如果“name”的值为“A”,则“testA”应为必填字段,如果“name”的值为“B”,则“testB”应为必填字段。
这是我尝试过的 Json 架构,但它没有按预期工作,
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [
"results"
],
"properties": {
"results": {
"type": "array",
"oneOf": [
{
"$ref": "#/definitions/person"
},
{
"$ref": "#/definitions/company"
}
]
}
},
"definitions": {
"person": {
"type": "object",
"required": [
"name",
"testA"
],
"properties": {
"name": {
"type": "string",
"enum": [
"A"
]
},
"testA": {
"type": "string"
}
}
},
"company": {
"type": "object",
"required": [
"name",
"testB"
],
"properties": {
"name": {
"type": "string",
"enum": [
"B"
]
},
"testB": {
"type": "string"
}
}
}
}
}
尝试过“依赖关系”也在 JSON Schema 中,但无法找到正确的解决方案。
任何使用示例 JSON 架构来实现上述用例的帮助/解决方法都值得赞赏。