Semantics

A formal specification of how JSON Documents validate against a JSON Schema Document

In this section we present a formal specification of how JSON Schema restrictions are validated against an arbitraty JSON Document. The following rules for formal validations are general, and thus there is no difference between those schemas whose type is specified and that whose type is not specified.

But before specifying the semantics for these validation instances we must define a couple of structures first.

Definitions

Let J be a JSON document. We say that a keyword k appears in J is J contains a key:value pair of the form k: j', for some document j'.

For a JSON Schema S, we use properties(S) to denote all keywords k1, ..., kn that appear in the key-value pair of the form "properties": {k1: s1 , ... , kn: sn} in S. The set properties(S) is empty if the keyword properties does not appear in S. Likewise, we use patternProperties(S) to denote all keywords k1, ..., kn that appear in the key-value pair of the form "patternProperties": {k1: s1 , ... , kn: sn} in S. The set patternProperties(S) is empty if the keyword properties does not appear in S.

Next, consider the following compatibility table:

type compatible keywords
string "type", "minLength", "maxLength", "pattern"
number "type", "minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"
integer "type", "minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"
boolean "type"
null "type"
array "type", "minItems", "maxItems", "items", "uniqueItems"
object "type", "properties", "additionalProperties", "required", "minProperties", "maxProperties", "dependencies", "patternProperties".

We define the function Compatible(J,k) as the function that receives a JSON document J and a key/value pair k of the form key: value and returns true if key is a string in the following list:

list of keywords
"type", "minLength", "maxLength", "pattern", "minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf", "minItems", "maxItems", "items", "uniqueItems", "properties", "additionalProperties", "required", "minProperties", "maxProperties", "dependencies", "patternProperties".

and key is compatible with the type of J according to the compatibility table above. Otherwise, Compatible(J,k) returns false. For instance, Compatible(42,"multipleOf": 7) = true, while Compatible(42,"minitems": 4) = false.

JSON Reference

If path is a JSON pointer, we say that rep(path) is the string resulting of replacing first each character ~1 by / and then each character ~0 by ~.

Given a JSON document J that is an object, we use J[k] (for a string k) to represent the value of the key value pair in J whose key is k. Likewise, if J is an array, then J[n] (for a natural number n) corresponds to the n-th element of J.

Let J be a JSON document. JSON Pointers are intended to extract a part of J that is specifically indexed by the pointer. Formally, we define the function EVAL() that takes a JSON Document J and a JSON Pointer JPointer and delivers a subset of J:

EVAL(J,JPointer) returns:

Let R be a JSON Reference of the form "$ref": uriRef. We extend the function EVAL() to work with arbitrary JSON references. We do it as follows.

EVAL(J,R) returns:

Let R be again a JSON Reference and J a JSON document. Assume that the JSON schema document that contains R is S. Then we say that J validates against R under S if EVAL(S,R) returns a schema (not an error) and J validates against EVAL(S,R).

Validation

Let S be a JSON Schema and J a JSON document, and assume that the JSON schema document that contains S is D. We say that J validates against S if one of the following holds:

Note how we make a special case when J is a JSON reference. In this case, but only if the reference sucessfully retrieves a schema, we ignore all the remaining keywords, and just validate against the schema retrieved by the reference. On the other hand, no document validates agains a reference that returns an error (simply because in this case it is not possible to instantiate the schema)

In turn, let S be a JSON schema, k be a key:value pair in S and J a JSON document. We say that J validates against k under S if one of the following holds:

General Restrictions

Strings Restrictions

Numeric Restrictions

Arrays Restrictions

Object Restrictions

Combinations