是否可以在 JSON Schema 中使用替代方案?在 XSD 中,这可以使用xs:alternative
元素。
例如参见:如何在 XML Schema 1.1 中使用替代方案 https://stackoverflow.com/questions/14593284/how-to-use-alternatives-in-xml-schema-1-1
更新1:
这是我想使用 JSON 模式描述的示例 JSON:
{
"actions": [
{
"type": "basic",
"param1": "value"
},
{
"type": "extended",
"param1": "value",
"param2": "blah"
}
]
}
要求:
-
actions
可能有任意数量的项目
-
basic
行动必须包含param1
财产
-
extended
行动必须包含param1
and param2
特性
自 Draft04 以来就有了类似的机制,具有更好的语义:oneOf、anyOf、allOf 和 not 关键字。
- oneOf:强制给定元素仅满足模式列表中的一个。
- anyOf:必须满足至少一个模式列表。
- allOf:必须满足列表中提供的所有模式。
- not:不能满足给定的模式。
假设您正在寻找一种独特的“替代方案”,这将是使用 oneOf 的 json 模式的示例:
{
"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"]
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)