Array Type Validation with JSON Schema

Validate arrays in JSON Schema with items, minItems, maxItems, and uniqueItems. Learn to constrain both the array itself and its individual elements.

Basic Types

JSON Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 50
      },
      "minItems": 1,
      "maxItems": 10
    },
    "scores": {
      "type": "array",
      "items": {
        "type": "number",
        "minimum": 0,
        "maximum": 100
      }
    }
  },
  "required": ["tags"]
}

Test Data

{
  "tags": ["javascript", "typescript", "react"],
  "scores": [95.5, 87, 100, 72.3]
}

Detailed Explanation

Validating Arrays in JSON Schema

The array type validates that a value is a JSON array. JSON Schema provides keywords to constrain both the array structure (length, uniqueness) and the type of each element.

Core Array Keywords

{
  "type": "array",
  "items": { "type": "string" },
  "minItems": 1,
  "maxItems": 10,
  "uniqueItems": true
}
  • items — a schema that every element must satisfy. If items is a single schema, it applies uniformly to all elements.
  • minItems — minimum number of elements. Set to 1 to forbid empty arrays.
  • maxItems — maximum number of elements. Use this to prevent oversized payloads.
  • uniqueItems — when true, all elements must be distinct. Comparison uses deep equality.

How the Example Schema Works

The schema defines two array properties:

  1. tags is a required array of strings. Each tag must be 1 to 50 characters long, and the array must contain between 1 and 10 items. This prevents empty tag lists and overly long tags.
  2. scores is an optional array of numbers, each between 0 and 100. There are no length constraints, so it can be empty or hold hundreds of values.

The test data passes because:

  • ["javascript", "typescript", "react"] has 3 items (within 1-10), and each string satisfies the length constraints.
  • [95.5, 87, 100, 72.3] contains numbers all within the 0-100 range.

Nested Array Validation

You can nest schemas to validate arrays of objects:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "name": { "type": "string" }
    },
    "required": ["id", "name"]
  }
}

This validates that every element is an object with id and name fields — a common pattern for list API responses.

Performance Considerations

For very large arrays (thousands of elements), validation can be expensive because the validator must check each element against the items schema. If performance is critical, consider setting a reasonable maxItems limit and validating a sample on the client side before submitting to the full validator.

Use Case

Use array validation for tagging systems, bulk API endpoints, list responses, multi-select form fields, and batch operations. Length constraints protect against payload abuse, while item schemas ensure every element in the array is well-formed.

Try It — JSON Schema Validator

Open full tool