This document describes the TypeSchema specification. TypeSchema is a JSON format to model data structures. It abstracts common OOP concepts like inheritance, polymorphism and generics into a simple and deterministic JSON format which can be transformed into code for many different programming languages. The main use case of TypeSchema is to describe data models, it is not designed to validate JSON structures. A data model described in TypeSchema can be used as single source of truth, which can be used across many different environments.
TypeSchema distinguishes between two types, a Definition-Type and a Property-Type.
A Definition-Type is an entry under the definitions
keyword and a Property-Type can only be used inside such a Definition-Type.
The following example illustrates how the Definition-Types and Property-Types are nested.
{ "definitions": { "TypeA": { "type": "struct", "properties": { "PropertyA": { Property-Type } } }, "TypeB": { "type": "map", "schema": { Property-Type } }, "TypeC": { Definition-Type } }, "root": "TypeA" }
A struct represents a class/structure with a fix set of defined properties.
{ "type": "struct", "parent": { Reference-Type }, "base": true, "properties": { "PropertyA": { Property-Type } }, "discriminator": "type", "mapping": { "CatType": "cat", "DogType": "dog", } }
Property | Description |
---|---|
parent | Defines a parent type for this structure. Some programming languages like Go do not support the concept of an extends , in this case the code
generator simply copies all properties into this structure. |
base | Indicates whether this is a base structure, default is false . If true the structure is used a base type, this means it is not possible
to create an instance from this structure. |
properties | Contains a map where they key is the property name and the value must be a Property-Type. |
discriminator | Optional the property name of a discriminator property. This should be only used in case this is also a base structure. |
mapping | In case a discriminator is configured it is required to configure a mapping. The mapping is a map where the key is the type name and the value the actual discriminator type value. |
A map represents a map/dictionary with variable key/value entries of the same type.
{ "type": "map", "schema": { Property-Type } }
Property | Description |
---|---|
schema | The Property-Type which defines the value of the map. |
An array represents an array/list with variable entries of the same type.
{ "type": "array", "schema": { Property-Type } }
Property | Description |
---|---|
schema | The Property-Type which defines the entry of the array. |
{ "type": "string", "format": "date-time" }
Property | Description |
---|---|
format | Optional describes the format of the string. Supported are the following types: date , date-time and time .
A code generator may use a fitting data type to represent such a format, if not supported it should fall back to a string. |
{ "type": "integer" }
{ "type": "number" }
{ "type": "boolean" }
A map represents a map/dictionary with variable key/value entries of the same type. The code generator uses the native map/dictionary type of the programming language.
{ "type": "map", "schema": { Property-Type } }
Property | Description |
---|---|
schema | The Property-Type which defines the value of the map. |
An array represents an array/list with variable entries of the same type. The code generator uses the native array/list type of the programming language.
{ "type": "array", "schema": { Property-Type } }
Property | Description |
---|---|
schema | The Property-Type which defines the entry of the array. |
{ "type": "any" }
{ "type": "generic", "name": "T" }
Property | Description |
---|---|
name | The name of the generic, it is recommended to use common generic names like T or TValue . These generics
can then be replaced on usage with a concrete type through the template property at a reference. |
{ "type": "reference", "target": "TypeB", "template": { "T": "TypeC" } }
Property | Description |
---|---|
target | The target type, this must be a key which is available under the definitions keyword. |
template | A map where the key is the name of the generic and the value must point to a key under the definitions keyword.
This can be used in case the target points to a type which contains generics, then it is possible to replace those generics
with a concrete type. |
Optional it is possible to import other TypeSchema documents through the import
keyword. It contains a map
where the key is the namespace and the value points to a remote document. The value is a URL and a code generator should
support at least the following schemes: file
, http
, https
.
{
"import": {
"MyNamespace": "file:///my_schema.json"
}
}
Inside a reference it is then possible to reuse all types under the namespace which are defined at the remote document i.e.:
{
"type": "reference",
"target": "MyNamespace:MyType"
}
In some circumstances a parse needs to know the root type of your specification, through the root
keyword it is possible to define such a root type.
{
"definitions": {
"TypeA": { ... }
},
"root": "TypeA"
}