Tagged union is a way to define a data type that can have different but rigid structure for different purposes. It is useful e.g. in command lists to devices, and event reports from devices. Different commands for different actions require different kind but rigidly defined parameters, and different kinds of events produce different kind of data. Many programming languages support tagged unions (with different names e.g. discriminated unions). The Wikipedia article
Tagged union describes the subject more thoroughly.
This is an example of a possible definition for variants:
{
"type": "object", "tag": {
"myTag": {
"type": "string",
"enum": ["move", "msg"]
},
},
"properties":{
"id": { "type": "integer" }
},
"variants":[
{
"tagValue": "move",
"properties": {
"x": { "type": "number" },
"y": { "type": "number" }
}
},
{
"tagValue": "msg",
"properties": {
"data": { "type": "string" }
}
}
],
"additionalProperties": false
}
This would pass the following records:
{ "myTag": "move", "id": "2780", "x": 758.32, "y": 334.28 }
{ "myTag": "msg", "id": "2780", "data": "2A7F4A42B9022" }
but not:
{ "myTag": "move", "id": "2780", "data": "2A7F4A42B9022" }
Ari Okkonen