Input validation
Validate request payloads containing lists of strings, numbers, or structured records.
Zod · Module
.array() turns an existing Zod schema into an array schema, so each item is validated against that schema. Use array length methods such as .min(), .max(), .length(), and .nonempty() for collection constraints.
.array() creates a new array schema whose elements must satisfy the schema it is called on. Array-specific constraints can then be chained, while the order of .array() and methods such as .optional() changes the resulting schema; the inner schema is available through .unwrap().
elementSchema.array()| Name | Type | Description |
|---|---|---|
elementSchema | Zod schema | The schema that every array element must satisfy. |
A Zod array schema whose elements conform to the receiver or supplied element schema.
z.array(elementSchema) or use the equivalent elementSchema.array() convenience method..array() method..unwrap() on an array schema when you need the schema governing individual elements..nonempty() to require at least one element..min(n) to enforce a minimum array length..max(n) to enforce a maximum array length..length(n) when the array must contain exactly a specified number of items.import { z } from "zod";
const tags = z.array(z.string()).min(1);
const result = tags.safeParse(["typescript", "zod"]);
if (result.success) {
console.log(result.data);
}Defines a schema for a non-empty array whose elements are strings, then safely validates an example value.
Validate request payloads containing lists of strings, numbers, or structured records.
Require collection sizes such as non-empty tags or bounded batches.
Reuse an existing element schema while composing nested array types.
Use either z.array(z.string()) or z.string().array(); both define an array whose items are strings. The method form is a convenience attached to the element schema.
Follow-up: What changes between z.string().optional().array() and z.string().array().optional()? The first permits undefined elements, while the second permits the entire array to be undefined.
Array schemas support .min(), .max(), and .length() for lower, upper, and exact item counts. .nonempty() requires at least one item.
Follow-up: How would you require between five and ten items? Chain .min(5).max(10) on the array schema.
Use .unwrap() to retrieve the schema used for each array element. For example, z.array(z.string()).unwrap() returns the string schema.
Follow-up: Why is this useful? It lets code inspect or reuse the inner schema without reconstructing the array definition.
z.arrayZodArray.unwrap().nonempty().min().max().length()