JSON Schema Validation for JSON Array Values

Q

What validation keywords I can use in JSON Schema to specifically validate JSON Array values?

✍: FYIcenter.com

A

The current JSON Schema specification supports the following validation keywords to specifically validate JSON Array values. They are not applied if the JSON instance is not a JSON Array.

"items" - If the JSON instance is an array, its elements must be valid against given schemas.

If a single schema is given in "items", all elements in the JSON instance array must be valid against the given schema. For example:

JSON Schema: 
{"items": 
    {"type": "string"}
}

Valid JSON instance:
["1", "2", "3"]

Valid JSON instance:
3.14159

Invalid JSON instance:
[1,2,3]

If an array of schemas is given in "items", for each given schema, the element in the JSON instance array at the same index position must be valid against the schema. Additional elements in the JSON instance array are valid by default. For example:

JSON Schema: 
{"items": 
    [
      {"type": "string"},
      {"type": "number"}
  ]
}

Valid JSON instance:
["age",25,true]

Valid JSON instance:
{"age":25}

Invalid JSON instance:
["age","25",true]

"additionalItems" - If the JSON instance is an array, and it have more elements than what "items" provides, additional elements are validated against the given schema. For example:

JSON Schema: 
{"items": 
    [
      {"type": "string"},
      {"type": "number"}
  ],
 "additionalItems": 
    {"type": "boolean"}
}

Valid JSON instance:
["age",25,true]

Valid JSON instance:
3.14159

Invalid JSON instance:
["age",25,"true"]

"additionalItems" is behave like "items", if "items" is not specified. For example:

JSON Schema: 
{"additionalItems": 
    {"type": "number"}
}

Valid JSON instance:
[1, 2, 3]

Invalid JSON instance:
["age",25,true]

"additionalItems" is ignored, if "items" is specified with a single schema. For example:

JSON Schema: 
{"items": 
    {"type": "string"},
 "additionalItems": 
    {"type": "number"}
}

Valid JSON instance:
["One","Two","Three"]

Invalid JSON instance:
[1, 2, 3]

"maxItems" - If the JSON instance is an array, the number of its elements must be less than or equal to the given value. For example:

JSON Schema: 
{"maxItems": 3}

Valid JSON instance:
["One","Two","Three"]

Valid JSON instance:
123

Invalid JSON instance:
["One","Two","Three", "Four"]

"minItems" - If the JSON instance is an array, the number of its elements must be greater than or equal to the given value. For example:

JSON Schema: 
{"minItems": 3}

Valid JSON instance:
["One","Two","Three"]

Valid JSON instance:
123

Invalid JSON instance:
["One","Two"]

"uniqueItems" - If the JSON instance is an array, the uniqueness of its elements must match the given boolean value For example:

JSON Schema: 
{"uniqueItems": true}

Valid JSON instance:
["One","Two","Three"]

Valid JSON instance:
123

Invalid JSON instance:
[9,1,1]

"contains" - If the JSON instance is an array, it must contains at least one element that is valid against the given schema. For example:

JSON Schema: 
{"contains": 
    {"type": "number"}
}

Valid JSON instance:
["One",2,"Three"]

Valid JSON instance:
"Hi"

Invalid JSON instance:
["One","Two","Three"]

 

JSON Schema Validation for JSON Object Values

JSON Schema Validation for JSON String Values

Introduction of JSON Schema

⇑⇑ JSON Tutorials

2017-09-01, 4463🔥, 0💬