Reputation: 1284
Is it possible to use alternatives in JSON Schema? In XSD this is doable using xs:alternative
element.
For example see: How to use alternatives in XML Schema 1.1
UPDATE 1:
This is a sample JSON I would like to describe using JSON schema:
{
"actions": [
{
"type": "basic",
"param1": "value"
},
{
"type": "extended",
"param1": "value",
"param2": "blah"
}
]
}
Requirements:
actions
may have any number of itemsbasic
actions must contain param1
propertyextended
actions must contain param1
and param2
propertiesUpvotes: 4
Views: 2187
Reputation: 13607
There is a similar mechanism since Draft04 with nicer semantics: oneOf, anyOf, allOf and not, keywords.
Assuming you are looking for an exclusive "alternative", this would be an example of json-schema using oneOf:
{
"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"]
}
}
}
Upvotes: 3