How can I hint the type of embedded objects in JSON Schema, analogous to xsi:type
in XML Schema?
Example schema document:
{
"type": "storeRequest",
"properties": {
"txid": {
"description": "Transaction ID to prevent double committing",
"type": "integer"
},
"objects": {
"description": "Objects to store",
"type": "array"
"items": {
"type": "object"
},
"minItems": 1,
"uniqueItems": true
},
},
"required": ["txid", "objects"]
}
This is a request the client sends to the server to store multiple objects in the database. Now how can I recursively validate the content of objects when it can contain more than one type of object. (Plymorphism, really).
There is not an equivalent to
xsi:type
inJSON-schema
AFAIK. Perhaps the mostJSON-schema
idiomatic way to hint the existence of types would be the explicit definition of types as schemas and referencing them through$ref
:Another way could be to give a hint through enums :
Finally you can also add whatever tag you can process to a
JSON-schema
and follow your conventions.Be aware that you are not going to find an equivalent translation between typical object oriented programming languages (
java
,C#
) inheritance semantics andJSON-schema
.