Validate collection inputs
Validate JSON or API payloads containing lists of primitive values or structured records.
Zod · Type Alias
ZodArray is the schema type created for arrays with z.array(elementSchema) or elementSchema.array(). It validates each item and supports array length constraints such as nonempty, min, max, and length.
ZodArray is the array schema produced by wrapping an element schema with z.array() or an element schema’s .array() method. It validates each item against the inner schema and can apply array-level size constraints such as nonempty, min, max, and length.
z.array(elementSchema)
elementSchema.array()| Name | Type | Description |
|---|---|---|
elementSchema | ZodType | The Zod schema each array element must satisfy. |
A ZodArray schema that validates arrays and their elements.
import { z } from "zod";
const tags = z.array(z.string()).min(1).max(3);
console.log(tags.parse(["typescript", "zod"]));
Creates an array schema whose elements are strings and whose length must be between one and three items.
Validate JSON or API payloads containing lists of primitive values or structured records.
Reject empty lists or enforce lower and upper bounds on submitted items.
Compose nested schemas for arrays of objects, unions, or other arrays.
Use z.array(elementSchema) to construct an array schema. The equivalent shorthand is elementSchema.array(), such as z.string().array() for an array of strings.
Follow-up: How would you restrict the number of elements? Chain an array-specific constraint such as .min(), .max(), .length(), or .nonempty() after creating the array schema.
The schema passed to z.array() describes each array element. For example, z.array(z.string()) accepts only arrays whose items validate as strings.
Follow-up: How can you inspect the element schema? Use the array schema’s inner-schema access described by the API documentation.
Zod provides array-specific validations for requiring at least one item, setting minimum or maximum size, and requiring an exact length. These are expressed with .nonempty(), .min(), .max(), and .length().
Follow-up: What happens if a value violates either the item schema or an array-size constraint? Parsing fails validation rather than returning the invalid value.
z.arrayZodStringnonemptyminmaxlengthZodObject