【发布时间】:2017-06-22 12:47:36
【问题描述】:
我有如下的输入 json,
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}
条件是, 如果类型是 'field',则 'itemId' 应该是必填字段 如果类型是'fieldGroup'或'subSection',则'itemId'是可选的
这是我尝试过的 Json Schema,但它没有按预期工作,
"type": "object",
"additionalProperties": false,
"properties" : {
"contents" : {
"type" : "array",
"items": {"$ref": "#displayItem" }
}
},
"definitions": {
"displayItem" : {
"id": "#displayItem",
"type": "object",
"items": {
"anyOf": [
{"$ref": "#fieldType"},
{"$ref": "#fieldGroupSubSectionType"}
]
}
},
"fieldType" : {
"id": "#fieldType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["field"]
}
}
},
"fieldGroupSubSectionType" : {
"id": "#fieldGroupSubSectionType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": [ "string", "null" ]
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
}
感谢任何使用 Sample Json Schema 实现上述用例的帮助/解决方法。
【问题讨论】:
标签: json jsonschema json-schema-validator