您可以使用
anyOf
关键字。
如果实例成功地针对由该关键字的值定义的至少一个模式进行了验证,则该实例将成功地针对该关键字进行验证。
http://json-schema.org/latest/json-schema-validation.html#anchor85
您需要定义这两种类型的项目,然后使用
任意的
描述“结果”的数组项。
{
"type": "object",
"properties": {
"code": { "type": "integer" },
"results": {
"type": "array",
"items": { "$ref": "#/definitions/resultItems" }
}
},
"definitions": {
"resultItems": {
"type": "object",
"anyOf": [
{ "$ref": "#/definitions/type1" },
{ "$ref": "#/definitions/type2" }
]
},
"type1": {
"properties": {
"type": { "enum": [1] },
"abc": { "type": "integer" }
},
"required": ["abc"]
},
"type2": {
"properties": {
"type": { "enum": [2] },
"def": { "type": "integer" }
},
"required": ["def"]
}
}
}