Finding JSON schema violations as a result of validating JSON value against JSON schema
Schema
flowchart LR subgraph inputs input_desc_0["JSON value"] input_desc_1["JSON schema"] end subgraph output output_desc["JSON schema violations"] end inputs --> output
Context
JSON validation is a specification for validating the structure and data
types of JSON values.
It allows you to specify the required properties, the types of values,
the format of the data, and other constraints for a JSON object.
This is useful for ensuring that the data received or sent in a JSON
format is as expected and can be processed correctly.
It helps to catch errors early, improve data quality, and reduce the
amount of code needed for data validation.
Properties
-
'true' JSON schema does not impose any constraints
-
'false' JSON schema rejects anything
-
any JSON value passes validation against 'empty object' JSON schema
Examples
finding JSON schema violations as a result of validating a multiple of x against schema accepting only numbers which are multiples of x
Because a JSON number value is a multiple of the factor desired by the schema, such a value is valid.
Input:
a multiple of x:
7.5
schema accepting only numbers which are multiples of x:
{ "multipleOf": 2.5, "type": [ "number" ] }
Output:
no violations:
∅
finding JSON schema violations as a result of validating a boolean value against schema accepting only nulls or strings
Because the value is neither null or string, such a value is invalid.
Input:
a boolean value:
true
schema accepting only nulls or strings:
{ "type": [ "null", "string" ] }
Output:
a type mismatch violation:
JSON value path:
$
JSON schema path:#/type
Invalid type. Expected null or string but got boolean.
finding JSON schema violations as a result of validating JSON number value against JSON schema accepting only numbers
Because a JSON value directly matches schema's only 'type' keyword item, such a value is valid.
Input:
JSON number value:
2.5
JSON schema accepting only numbers:
{ "type": [ "number" ] }
Output:
no violations:
∅
finding JSON schema violations as a result of validating JSON null value against JSON schema accepting booleans, nulls and strings
Because a JSON value directly matches one of schema's 'type' keyword items, such a value is valid.
Input:
JSON null value:
null
JSON schema accepting booleans, nulls and strings:
{ "type": [ "boolean", "null", "string" ] }
Output:
no violations:
∅
finding JSON schema violations as a result of validating JSON number value which happens to be an integer against JSON schema accepting any numbers
Because a JSON value indirectly matches schema's only 'type' keyword item, such a value is valid.
Input:
JSON number value which happens to be an integer:
1
JSON schema accepting any numbers:
{ "type": [ "number" ] }
Output:
no violations:
∅
finding JSON schema violations as a result of validating a fractional number against schema accepting only whole numbers
Because the schema accepts only whole numbers, such a value is invalid.
Input:
a fractional number:
1.5
schema accepting only whole numbers:
{ "type": [ "integer" ] }
Output:
a type mismatch violation:
JSON value path:
$
JSON schema path:#/type
Invalid type. Expected integer but got number.
finding JSON schema violations as a result of validating a multiple of 2.5 against a schema accepting only multiples of 2.5
Because the schema accepts any multiples of 2.5, such a value is valid.
Input:
a multiple of 2.5:
7.5
a schema accepting only multiples of 2.5:
{ "multipleOf": 2.5, "type": [ "number" ] }
Output:
no violations:
∅
finding JSON schema violations as a result of validating number at the schema's maximum allowed values boundary against a schema with a maximum exclusive allowed value set
Because the maximum value constraint is exclusive, such a value is invalid.
Input:
number at the schema's maximum allowed values boundary:
4
a schema with a maximum exclusive allowed value set:
{ "exclusiveMaximum": 4 }
Output:
an invalid range violation:
JSON value path:
$
JSON schema path:#/exclusiveMaximum
4.0 is outside of the valid range of
(-Infinity,4.0)
finding JSON schema violations as a result of validating an array containing some duplicated strings against schema not accepting duplicates
Because the schema requires items to be unique, and the value contains duplicate occurrence, such a value is invalid.
Input:
an array containing some duplicated strings:
[ "a", "b", "b", "c", "d", "d", "e" ]
schema not accepting duplicates:
{ "uniqueItems": true }
Output:
an invalid array violation:
JSON value path:
$
JSON schema path:#
Invalid array:
JSON value path:
$[1]
JSON schema path:#/uniqueItems
Non-unique array item.
JSON value path:
$[2]
JSON schema path:#/uniqueItems
Non-unique array item.
JSON value path:
$[4]
JSON schema path:#/uniqueItems
Non-unique array item.
JSON value path:
$[5]
JSON schema path:#/uniqueItems
Non-unique array item.
finding JSON schema violations as a result of validating an array containing a mixture of null and boolean values to a schema accepting only arrays of nulls against schema accepting only arrays of nulls
Because the schema requires items to conform to a certain schema and this is not the case here, such a value is invalid.
Input:
an array containing a mixture of null and boolean values to a schema accepting only arrays of nulls:
[ null, false, null, true, null ]
schema accepting only arrays of nulls:
{ "items": { "type": [ "null" ] }, "type": [ "array" ] }
Output:
an invalid array violation:
JSON value path:
$
JSON schema path:#
Invalid array:
JSON value path:
$[1]
JSON schema path:#/items/type
Invalid type. Expected null but got boolean.
JSON value path:
$[3]
JSON schema path:#/items/type
Invalid type. Expected null but got boolean.
finding JSON schema violations as a result of validating not a multiple of 2.5 against a schema accepting only multiples of 2.5
Because the schema accepts only multiples of 2.5, such a value is invalid.
Input:
not a multiple of 2.5:
7
a schema accepting only multiples of 2.5:
{ "multipleOf": 2.5, "type": [ "number" ] }
Output:
an invalid multiple violation:
JSON value path:
$
JSON schema path:#/multipleOf
7.0 is not a multiple of 2.5
finding JSON schema violations as a result of validating number below the schema's maximum allowed values boundary against a schema with a maximum exclusive allowed value set
Because the value is less than the maximum value constraint, such a value is valid.
Input:
number below the schema's maximum allowed values boundary:
3
a schema with a maximum exclusive allowed value set:
{ "exclusiveMaximum": 4 }
Output:
no violations:
∅
finding JSON schema violations as a result of validating number at the schema's minimum allowed values boundary against a schema with a minimum exclusive allowed value set
Because the minimum value constraint is exclusive, such a value is invalid.
Input:
number at the schema's minimum allowed values boundary:
4
a schema with a minimum exclusive allowed value set:
{ "exclusiveMinimum": 4 }
Output:
an invalid range violation:
JSON value path:
$
JSON schema path:#/exclusiveMinimum
4.0 is outside of the valid range of
(4.0,Infinity)
finding JSON schema violations as a result of validating number at the schema's maximum allowed values boundary against a schema with a maximum inclusive allowed value set
Because the maximum value constraint is inclusive, such a value is valid.
Input:
number at the schema's maximum allowed values boundary:
4
a schema with a maximum inclusive allowed value set:
{ "maximum": 4 }
Output:
no violations:
∅
finding JSON schema violations as a result of validating number at the schema's minimum allowed values boundary against a schema with a minimum inclusive allowed value set
Because the minimum value constraint is inclusive, such a value is valid.
Input:
number at the schema's minimum allowed values boundary:
4
a schema with a minimum inclusive allowed value set:
{ "minimum": 4 }
Output:
no violations:
∅
finding JSON schema violations as a result of validating number exceeding the schema's maximum allowed values boundary against a schema with a maximum inclusive allowed value set
Because the value is greater than the maximum value constraint is exclusive, such a value is invalid.
Input:
number exceeding the schema's maximum allowed values boundary:
5
a schema with a maximum inclusive allowed value set:
{ "maximum": 4 }
Output:
an invalid range violation:
JSON value path:
$
JSON schema path:#/maximum
5.0 is outside of the valid range of
(-Infinity,4.0]
finding JSON schema violations as a result of validating number below the schema's minimum allowed values boundary against a schema with a minimum exclusive allowed value set
Because the value is less than the minimum value constraint, such a value is valid.
Input:
number below the schema's minimum allowed values boundary:
5
a schema with a minimum exclusive allowed value set:
{ "exclusiveMinimum": 4 }
Output:
no violations:
∅
finding JSON schema violations as a result of validating number exceeding the schema's minimum allowed values boundary against a schema with a minimum inclusive allowed value set
Because the value is greater than the minimum value constraint is exclusive, such a value is invalid.
Input:
number exceeding the schema's minimum allowed values boundary:
3
a schema with a minimum inclusive allowed value set:
{ "minimum": 4 }
Output:
an invalid range violation:
JSON value path:
$
JSON schema path:#/minimum
3.0 is outside of the valid range of
[4.0,Infinity)