After working for a while with JSON schemas that represent an Object-Oriented model instance, I have realized how painful and exhausting it is to duplicate properties everywhere when there is inheritance, fighting with discriminators to make the schema validate against the right type, and the worst: always needing to transform a graph of objects into a tree to match the JSON structure.
So I decided to test another idea: make a JSON schema that is actually designed for Object Oriented Models. So you get the same kind of validation as a normal JSON schema, but actually supporting:
- inheritance
- abstract types
- has-one and has-many relationships (non-hierarchical like JSON parent-child, but horizontal relationships)
While also supporting:
- same native types and constraints as JSON Schema
- hierarchical relationships (“vertical” like JSON)
- the OO JSON Schema (OOJS) itself is a JSON and it’s represented by a normal JSON Schema
The validation process could be a little faster in OOJS because it matches the specific type, instead of testing against all types to find matches, also the validation is polymorphic: if your schema says A has-one B and your data is a: A has-one c: C, if C is-a B, then your data validates. Also if your data has an object with a type that is abstract, validation will fail.
I’m working now on generating OOJS for the openEHR models, so I can benchmark the validation against the canonical JSON Schemas. Of course the JSON data instance structure that complies with the OOJS will have certain differences between the JSONs that comply with the canonical openEHR JSON Schema, though there should be a 1-to-1 mapping between the OOJS instances to the canonical openEHR JSON format, though it might not be bidirectional.
To give you an idea, the current openEHR OOJS for all models are about 72.1 KB, and the full canonical JSON Schema is about 637 KB: there is a lot of extra metadata there to make it work in an OO world!
You can find the project here GitHub - CaboLabs/json-object-schema: JSON Schema for Object-Oriented Models · GitHub
In the /examples folder we have the openEHR OO JSON Schemas.
We also have reference implementations in different programming languages.
BTW, I think besides data validation this is a great source for code generation (data/state only).