3 ms·
I must admit I'm a noob in this world so maybe I'm going about it the wrong way, but I used JSON Schema's oneOf[1] along with required[2] for this: "proper
by magicalhippo 4y ago
I must admit I'm a noob in this world so maybe I'm going about it the wrong way, but I used JSON Schema's oneOf[1] along with required[2] for this:
"properties": {
"cat": {
"$ref": "#/$defs/catType"
}
"dog": {
"$ref": "#/$defs/dogType"
}
},
"oneOf": [
{
"required": ["cat"]
},
{
"required": ["dog"]
},
]
Usage would then look like:
{
"dog": {
"name": "Charlie"
}
}
At least it's explicit and can be easily checked for validity.
[1]: https://json-schema.org/understanding-json-schema/reference/combining.html#oneof https://json-schema.org/understanding-json-schema/reference/...
[2]: https://json-schema.org/understanding-json-schema/reference/object.html#required-properties https://json-schema.org/understanding-json-schema/reference/...
- com2kid 4y agoYou are doing it correctly. https://en.wikipedia.org/wiki/Tagged_union https://en.wikipedia.org/wiki/Tagged_union