问题描述
我有输入JSON像下面,
I have the input json like below,
{
"results": [{
"name": "A",
"testA": "testAValue"
}]
}
的条件是,如果'名'的值是'A',然后'种皮'应该是必要的字段,如果'名'的值是'B',然后'TESTB'应该是必填字段。
the condition is, if value of 'name' is 'A', then 'testA' should be the required field and if value of 'name' is 'B', then 'testB' should be the required field.
这是JSON模式我想和它不工作不如预期,
This is the Json Schema I tried and its not working as expected,
{
"$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"
}
}
}
}
}
与尝试的依赖条件在JsonSchema也没能找到正确的解决方案。
Tried with dependecies in JsonSchema too not able to find the correct solution.
任何帮助/解决方法与样品JSON模式实现上述用例是AP preciated。
Any help / workaround with Sample Json Schema to achieve the above use case is appreciated.
在此先感谢,
哈里
Thanks in advance,Harry
推荐答案
Your're接近。你的 oneOf
必须在项目
关键字。
Your're close. Your oneOf
needs to be in the items
keyword.
{
"type": "array",
"items": {
"oneOf": [
{ "$ref": "#/definitions/person" },
{ "$ref": "#/definitions/company" }
]
}
}
这篇关于根据属性值条件JSON模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!