This document describes the TypeSchema specification. TypeSchema is a JSON format to model data structures. It abstracts OOP concepts into a simple and deterministic JSON format which can be turned into code for many different target languages. The main use case of TypeSchema is to describe a data model, 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 reused in many different environments.
Every TypeSchema has a Root definition. The Root must contain at least the
definitions
keyword i.e.:
{ "definitions": { "TypeA": { ... }, "TypeB": { ... } } }
The definitions
keyword contains simply a map containing Struct,
Map and Reference types.
Optional it is possible to include a $ref
keyword which points to the default type.
Optional it is possible to import other 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 URN and the supported
schemes i.e. file
, http
, https
etc. are out of bound of this specification.
{
"$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.:
{
"$ref": "MyNamespace:MyType"
}
At TypeSchema every type can be determined based on the used keywords. The following list describes every type and how to use them.
Represents a struct type. A struct type contains a fix set of defined properties. A struct type must have a
type
and properties
keyword. The type must be object
.
{
"type": "object",
"properties": {
"PropertyA": { ... },
"PropertyB": { ... }
}
}
All allowed properties are described at the Appendix.
Represents a map type. A map type contains variable key value entries of a specific type. A map type must have a
type
and additionalProperties
keyword. The type must be object
.
{
"type": "object",
"additionalProperties": { ... }
}
All allowed properties are described at the Appendix.
Represents an array type. An array type contains an ordered list of a specific type. An array type must have a
type
and items
keyword. The type must be array
.
{
"type": "array",
"items": { ... }
}
All allowed properties are described at the Appendix.
Represents a boolean type. A boolean type must have a type
keyword and the type must be
boolean
.
{
"type": "boolean"
}
All allowed properties are described at the Appendix.
Represents a number type (contains also integer). A number type must have a type
keyword and the type
must be number
or integer
.
{
"type": "number"
}
All allowed properties are described at the Appendix.
Represents a string type. A string type must have a type
keyword and the type must
be number
or integer
.
{
"type": "string"
}
All allowed properties are described at the Appendix.
Represents an intersection type. An intersection type must have an allOf
keyword.
{
"allOf": [{ ... }, { ... }]
}
All allowed properties are described at the Appendix.
Represents a union type. A union type must have an oneOf
keyword.
{
"oneOf": [{ ... }, { ... }]
}
All allowed properties are described at the Appendix.
Represents a reference type. A reference type points to a specific type at the definitions map. A reference type
must have a $ref
keyword.
{
"$ref": "MyType"
}
All allowed properties are described at the Appendix.
Represents a generic type. A generic type represents a type which can be replaced if you reference a specific type.
A generic type must have a $generic
keyword.
{
"$generic": "T"
}
All allowed properties are described at the Appendix.
I.e. if we reference a specific type and this type contains a generic type then we can define which type should be inserted at the generic type.
{
"$ref": "MyType",
"$template": {
"T": "AnotherType"
}
}
The following image shows how the types can be nested.
The single source of truth of TypeSchema is the TypeSchema meta schema which describes itself. You can find the current TypeSchema at our repository. The following section contains a HTML representation which we automatically generate from this meta schema.
{ "type": String, }
Field | Description |
---|---|
type | String
|
{ "type": String, "items": BooleanType | NumberType | StringType | ReferenceType | GenericType | AnyType, "maxItems": Integer, "minItems": Integer, }
Field | Description |
---|---|
type | String
|
items | BooleanType | NumberType | StringType | ReferenceType | GenericType | AnyType |
maxItems | Integer Positive integer value |
minItems | Integer Positive integer value |
{ "type": String, }
Field | Description |
---|---|
type | String
|
{ "description": String, "type": String, "nullable": Boolean, "deprecated": Boolean, "readonly": Boolean, }
Field | Description |
---|---|
description | String General description of this type, should not contain any new lines. |
type | String Type of the property
|
nullable | Boolean Indicates whether it is possible to use a null value |
deprecated | Boolean Indicates whether this type is deprecated |
readonly | Boolean Indicates whether this type is readonly |
{ "propertyName": String, "mapping": DiscriminatorMapping, }
Field | Description |
---|---|
propertyName | String The name of the property in the payload that will hold the discriminator value |
mapping | DiscriminatorMapping |
Map (String)
{ "$generic": String, }
Field | Description |
---|---|
$generic | String |
Map (String)
{ "description": String, "allOf": Array (ReferenceType), }
Field | Description |
---|---|
description | String |
allOf | Array (ReferenceType) Contains an array of references. The reference must only point to a struct type |
{ "type": String, "additionalProperties": BooleanType | NumberType | StringType | ArrayType | UnionType | IntersectionType | ReferenceType | GenericType | AnyType, "maxProperties": Integer, "minProperties": Integer, }
Field | Description |
---|---|
type | String
|
additionalProperties | BooleanType | NumberType | StringType | ArrayType | UnionType | IntersectionType | ReferenceType | GenericType | AnyType |
maxProperties | Integer Positive integer value |
minProperties | Integer Positive integer value |
{ "type": String, "multipleOf": Number, "maximum": Number, "exclusiveMaximum": Boolean, "minimum": Number, "exclusiveMinimum": Boolean, }
Field | Description |
---|---|
type | String
|
multipleOf | Number |
maximum | Number |
exclusiveMaximum | Boolean |
minimum | Number |
exclusiveMinimum | Boolean |
Map (BooleanType | NumberType | StringType | ArrayType | UnionType | IntersectionType | ReferenceType | GenericType | AnyType)
{ "$ref": String, "$template": TemplateProperties, }
Field | Description |
---|---|
$ref | String Reference to a type under the definitions map |
$template | TemplateProperties Optional concrete type definitions which replace generic template types |
{ "format": String, "enum": Array (String | Number), "default": String | Number | Boolean, }
Field | Description |
---|---|
format | String Describes the specific format of this type i.e. date-time or int64 |
enum | Array (String | Number) |
default | String | Number | Boolean |
{ "type": String, "maxLength": Integer, "minLength": Integer, "pattern": String, }
Field | Description |
---|---|
type | String
|
maxLength | Integer Positive integer value |
minLength | Integer Positive integer value |
pattern | String |
{ "$final": Boolean, "$extends": String, "type": String, "properties": Properties, "required": Array (String), }
Field | Description |
---|---|
$final | Boolean Indicates that a struct is final, this means it is not possible to extend this struct |
$extends | String Extends an existing type with the referenced type |
type | String
|
properties | Properties |
required | Array (String) |
Map (String)
{ "$import": Import, "definitions": Definitions, "$ref": String, }
Field | Description |
---|---|
$import | Import |
definitions | Definitions |
$ref | String Reference to a root schema under the definitions key |
{ "description": String, "discriminator": Discriminator, "oneOf": Array (NumberType | StringType | BooleanType | ReferenceType), }
Field | Description |
---|---|
description | String |
discriminator | Discriminator |
oneOf | Array (NumberType | StringType | BooleanType | ReferenceType) Contains an array of references. The reference must only point to a struct type |