items : Schema

items

Schema

Validation succeeds if each element of the instance not covered by prefixItems validates against this schema.

Kind Applicator Annotation
Applies To Array
Dialect 2020-12
Introduced In Draft 1
Vocabulary Applicator
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.1.2
Metaschema https://json-schema.org/draft/2020-12/meta/applicator
Affected By
Also see

Annotations

If this keyword is applied to any instance element, it produces an annotation value of true.

Explanation

The items keyword is used to validate arrays of arbitrary length where each item in the array matches the same schema. It applies its subschema to all instance items at indexes greater than the length of the prefixItems array in the same schema, or to all instance array elements if prefixItems is not present. This means that the subschema specified under items will be used to validate every item in the array that isn’t covered by prefixItems.

If the items subschema is applied to any positions within the instance array, it produces an annotation result of boolean true, indicating that all remaining array elements have been evaluated against this keyword’s subschema. This annotation affects the behavior of unevaluatedItems in the Unevaluated vocabulary.

  • The value of this keyword must be a valid JSON Schema.
  • prefixItems allows defining a fixed-length sequence of schemas for an array’s initial items.
  • items applies its sub-schema to all elements after the prefixItems sequence (if present).
  • Analogous to additionalProperties for objects, items specifies a schema that each item in the array must adhere to. If an array has additional items beyond what’s defined in prefixItems, they must conform to the schema specified under items.

Examples

Schema with 'items' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": { "type": "number" }
}
An array instance with all items as numeric values is valid Instance
[ 2, 3, 44, -5 ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/items",
    "instanceLocation": "",
    "annotation": true
  },
  // ...
]
An array instance containing a string value is invalid Instance
[ 2, 3, "44", -5 ]
Schema with 'items' set to boolean true Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": true
}
An array instance with all items as numeric values is valid Instance
[ 2, 3, 44, -5 ]
An array instance containing a string value is also valid Instance
[ 2, 3, "44", -5 ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/items",
    "instanceLocation": "",
    "annotation": true
  },
  // ...
]
  • Similarly, if the items is set to false, all the array instances will fail validation.
Schema with 'items' and 'prefixItems' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [
    { "type": "boolean" },
    { "type": "string" }
  ],
  "items": { "type": "number" }
}
An array instance adhering to the schema is valid Instance
[ false, "44", -5 ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/prefixItems",
    "instanceLocation": "",
    "annotation": 1
  },
  {
    "valid": true,
    "keywordLocation": "/items",
    "instanceLocation": "",
    "annotation": true
  },
  // ...
]
The prefix items of the array instance must adhere to the subschemas in 'prefixItems' at their respective indexes Instance
[ 2, 3, "44", -5 ]