Skip to main content

Lesson 4. Schemas

Example 4.1. Implicit type specification

TYPE @cat
{ # implicitly set `object`
"id" : 123, # implicitly set `integer`
"name" : "Tom", # implicitly set `string`
"isLovely" : true , # implicitly set `boolean`
"weight" : 7.1, # implicitly set `float`
"friendIds": [ # implicitly set `array` of `integer`s [0–∞]
232
],
"address": { # implicitly set `object`
"country": "USA", # implicitly set `string`
"city" : "New York", # implicitly set `string`
"line 1" : "Cats street, 23", # implicitly set `string`
"zipcode": 123456 # implicitly set `integer`
}
}

Note. The hash mark # is followed by comments.

In this example, we demonstrate data types that are implicitly but clearly defined from an example of valid data. These are the types: object, array, integer, float, string, boolean.

These types, if desired, can be specified explicitly using the rule type as follows:

TYPE @cat
{ // {type: "object"} # type is optional
"id": 123 // {type: "integer"} # type is optional
}
info

For more information about this topic, see:

Example 4.2. Other built-in types

TYPE @cat
{
"id" : 123,
"name" : "Tom",
"birthday" : "2006-01-02", // {type: "date"}
"registered": "2021-01-02T07:23:12+03:00", // {type: "datetime"}
"email" : "tom@cats.com", // {type: "email" }
"website" : "http://tom.cats.com", // {type: "uri" }
"salary" : 13.23, // {type: "decimal", precision: 2}
"color" : "white", // {type: "enum", enum: ["white", "black"]}
"uuid" : "550e8400-e29b-41d4-a716-446655440000" // {type: "uuid"}
}

JSight has several built-in types that must always be specified explicitly in the rule type. These types are depicted in the example to the left. These types include:

  • decimal,
  • date,
  • datetime,
  • email,
  • uri,
  • enum,
  • uuid.

Types decimal and enum always require additional rules (type decimal requires the rule precision, and type enum requires the rule enum).

Therefore, for these types, the rule type can be omitted, especially as it has no effect on perception:

TYPE @cat
{
"salary": 13.23, // {precision: 2} # type is `decimal`
"color" : "white" // {enum: ["white", "black"]} # type is `enum`
}
info

For more information about this topic, see:

Example 4.3. User types

TYPE @cat
{
"id" : @catId,
"name" : "Tom",
"friends": [@cat]
}

TYPE @catId
"CAT-123" // {regex: "^CAT-\\d+$"}

In the given example, we can see how user types can be specified in schemas:

In the type @cat the user type @catId is specified for the field "id" (line 3). In the type @cat the elements of array "friends" are assigned to the type @cat (line 5).

Example 4.4. User types – expressed with the example

TYPE @cat
{
"id" : "CAT-23454", // {type: "@catId"}
"name" : "Tom",
"friends": [@cat]
}

TYPE @catId
"CAT-123" // {regex: "^CAT-\\d+$"}

In the given example, the same type of data is provided, but we specify an example value for the field "id" in the type @cat, while the reference to the user data type was indicated in the annotation in the rule type (string 3).

This is convenient to do in the case of scalar types. This cannot be done with arrays and objects.

Example 4.5. Reference to multiple user types

TYPE @cat
{
"id" : 123,
"name": "Tom",
"closestFriend": @cat | @dog
}

In the given example, the value of the property "closestFriend" must be of type @cat or @dog.

A table of all built-in types and rules

The table below shows all types as well as all of the rules that can be applied to them.

TypeRules for this type
anyoptional, nullable
arrayoptional, nullable, minItems, maxItems
booleanoptional, nullable, const
dateoptional, nullable, const, regex
datetimeoptional, nullable, const, regex
decimaloptional, nullable, const, min, max, exclusiveMinimum, exclusiveMaximum, precision
emailoptional, nullable, const, regex
enumoptional, nullable, const, enum
floatoptional, nullable, const, min, max, exclusiveMinimum, exclusiveMaximum
integeroptional, nullable, const, min, max, exclusiveMinimum, exclusiveMaximum
mixedoptional, nullable, or
nulloptional, nullable, const
objectoptional, nullable, additionalProperties, allOf
stringoptional, nullable, const, minLength, maxLength, regex
urioptional, nullable, const, regex
uuidoptional, nullable, const
@UserTypeoptional, nullable