Producing a JSON schema or an error as a result of parsing JSON value
Schema
flowchart LR subgraph inputs input_desc_0["JSON value"] end subgraph output output_desc["a JSON schema or an error"] end inputs --> output
Context
JSON schema is commonly expressed in a JSON format. However, not every JSON is a valid JSON schema.
Examples
-
producing a JSON schema or an error as a result of parsing a boolean JSON value
-
producing a JSON schema or an error as a result of parsing an empty JSON object
producing a JSON schema or an error as a result of parsing a boolean JSON value
Because a boolean value of false is a valid schema which passes validation of any JSON value, such a value represents a JSON schema.
Input:
a boolean JSON value:
false
Output:
a successfully parsed JSON schema:
false
producing a JSON schema or an error as a result of parsing A JSON object with 'type' property set to an array with a single 'null' string inside it
Because the 'type' keyword defines acceptable JSON types. It can be in a form of an array of string (here, only with one type defined), such a value represents a JSON schema.
Input:
A JSON object with 'type' property set to an array with a single 'null' string inside it:
{ "type": [ "null" ] }
Output:
a successfully parsed JSON schema:
{ "type": [ "null" ] }
producing a JSON schema or an error as a result of parsing A JSON object with 'type' property defined
Because the 'type' keyword defines acceptable JSON types. It can be in a form of a single string, such a value represents a JSON schema.
Input:
A JSON object with 'type' property defined:
{ "type": "null" }
Output:
a successfully parsed JSON schema:
{ "type": [ "null" ] }
producing a JSON schema or an error as a result of parsing A JSON object with 'required' property being array of strings
Because the 'required' constrain rejects any JSON object not containing properties defined by it, such a value represents a JSON schema.
Input:
A JSON object with 'required' property being array of strings:
{ "required": [ "prop1", "prop2" ] }
Output:
a successfully parsed JSON schema:
{ "required": [ "prop1", "prop2" ] }
producing a JSON schema or an error as a result of parsing A JSON object with 'type' property set to an array with 'array', 'null' and 'string' strings inside it
Because the 'type' keyword defines acceptable JSON types. It can be in a form of an array of string (here, with three types defined), such a value represents a JSON schema.
Input:
A JSON object with 'type' property set to an array with 'array', 'null' and 'string' strings inside it:
{ "type": [ "array", "null", "string" ] }
Output:
a successfully parsed JSON schema:
{ "type": [ "array", "null", "string" ] }
producing a JSON schema or an error as a result of parsing A JSON object with 'uniqueItems' property set
Because the 'uniqueItems' keyword makes sure that if JSON value is an array, its items do not contain any duplicates, such a value represents a JSON schema.
Input:
A JSON object with 'uniqueItems' property set:
{ "uniqueItems": true }
Output:
a successfully parsed JSON schema:
{ "uniqueItems": true }
producing a JSON schema or an error as a result of parsing A JSON object with 'type' property set to an empty array
Because the 'type' keyword defines acceptable JSON types. It can be in a form of an array of string (here, with no types defined), such a value represents a JSON schema.
Input:
A JSON object with 'type' property set to an empty array:
{ "type": [] }
Output:
a successfully parsed JSON schema:
{ "type": [] }
producing a JSON schema or an error as a result of parsing a JSON value not being a boolean or object
Because booleans and objects are the only acceptable forms, such a value does not represent a JSON schema.
Input:
a JSON value not being a boolean or object:
0
Output:
a parsing error:
an error:
"the JSON value is neither a boolean nor an object"
producing a JSON schema or an error as a result of parsing an JSON object with the 'not' property defined
Because the 'not' constrain rejects any JSON value which conform to schema defined by it, such a value represents a JSON schema.
Input:
an JSON object with the 'not' property defined:
{ "not": true }
Output:
a successfully parsed JSON schema:
{ "not": true }
producing a JSON schema or an error as a result of parsing an JSON object with the 'items' property defined
Because the 'items' constrain makes sure than if a JSON value is an array, every item of that array conforms the schema defined by it, such a value represents a JSON schema.
Input:
an JSON object with the 'items' property defined:
{ "items": true }
Output:
a successfully parsed JSON schema:
{ "items": true }
producing a JSON schema or an error as a result of parsing an empty JSON object
Because an empty JSON object is a valid schema which passes validation of any JSON value, such a value represents a JSON schema.
Input:
an empty JSON object:
{}
Output:
a successfully parsed JSON schema:
{}