Array input validation
Validate request payloads containing lists of primitive values or structured records.
Zod · Function
z.array creates a schema for arrays whose elements conform to a supplied Zod schema. It also supports array-length constraints and readonly output types.
z.array builds an array schema from an element schema. Each array item is checked against that inner schema, while array-specific methods can constrain the number of items or produce a readonly array type.
z.array(element)| Name | Type | Description |
|---|---|---|
element | Zod schema | The schema that every element in the array must satisfy. |
An array schema whose elements are validated by the supplied element schema.
import { z } from "zod";
const Tags = z.array(z.string()).min(1).max(3);
const result = Tags.safeParse(["typescript", "zod"]);
if (result.success) {
console.log(result.data);
}Creates a schema for an array of strings, requires at least one item, and limits the array to three items.
Validate request payloads containing lists of primitive values or structured records.
Enforce collection cardinality such as nonempty, bounded, or exact-size lists.
Expose immutable array types after validation.
z.array(z.string()) creates a schema whose input must be an array and whose elements must each satisfy z.string(). The equivalent method form is z.string().array().
Follow-up: How would you require between two and five elements? Chain .min(2).max(5) onto the array schema.
Use .min(n) for a lower bound, .max(n) for an upper bound, and .length(n) for an exact count. For example, z.array(z.string()).min(5) requires five or more strings.
Follow-up: How do you reject an empty array without specifying a numeric lower bound? Use .nonempty().
The schema's unwrap() method returns the inner element schema. For const stringArray = z.array(z.string()), stringArray.unwrap() yields the string schema.
Follow-up: What type does readonly() produce for a string array? It produces a readonly string[] type.
z.string().array()unwrap()nonempty()min()max()length()readonly()z.tuple()