是否可以在 JSON Schema中使用替代方案?在XSD中,使用xs:alternative元素是可行的. 例如,请参阅:How to use alternatives in XML Schema 1.1 更新1: 这是我想用JSON模式描述的示例JSON: { "actions": [ {
例如,请参阅:How to use alternatives in XML Schema 1.1
更新1:
这是我想用JSON模式描述的示例JSON:
{
"actions": [
{
"type": "basic",
"param1": "value"
},
{
"type": "extended",
"param1": "value",
"param2": "blah"
}
]
}
要求:
>动作可以包含任意数量的项目
>基本操作必须包含param1属性
>扩展操作必须包含param1和param2属性
> oneOf:强制执行给定元素以仅满足模式列表中的一个.
> anyOf:必须满足至少一个模式列表.
> allOf:必须满足列表中提供的所有模式.
> not:不得满足给定的架构.
假设您正在寻找一个独有的“替代”,这将是使用oneOf的json-schema的示例:
{
"actions" : {
"type" : "array",
"items" : {
"oneOf" : [{
" $ref " : "#/definitions/type1 "
}, {
" $ref " : "#/definitions/type2 "
}
]
}
},
" definitions " : {
" type1 " : {
" type " : " object ",
"properties": {
"param1":{"type":"string"}
},
"required":["param1"]
},
" type2 " : {
" type " : " object ",
"properties": {
"param2":{"type":"string"},
"param3":{"type":"string"}
},
"required":["param2","param3"]
}
}
}
