Hi!
What is the best approach to describing data with type tags? (Or, alternatively, how to structure the data in such a way that type tags are not needed at all)
Example:
{“type”: “login”,
“username”: “foo”,
“timestamp”: 342123}
{“type”: “forgotPassword”,
“email”: “
f...@example.com”,
“timestamp”: 423421}
The following approach is the best I’ve found:
Main schema:
{“oneOf”: [{“$ref”: “login-event.json”},
{“$ref”: “forgot-password-event.json”}]}
login-event.json:
{“type”: “object”,
“properties”: {“type”: {“enum”: [“login”]},
“email”: {“type”: “string”},
“timestamp”: {“type: “number”}}}
forgot-password-event.json would of course look similar.
Is there a better way to create schemas for type tagged data (where the value of the type property determines the structure of the rest of the object)? Is it possible specify common properties (for example "timestamp" in the example) in the top level schema instead of in each sub schema?
I’ve also found that validation failure reports are extremely verbose when the top level schema is a long list of “oneOf” schemas.
Thanks,
Jonas