Code Reference

Arrays & Tuples

Zod · Reference cheat sheet

Arrays & Tuples

Zod · Reference cheat sheet


📋 Overview

Zod models homogeneous arrays with z.array and fixed-length positional collections with z.tuple. Useful for lists of DTOs and CSV-like rows.

🔧 Core concepts

SchemaMeaning
z.array(T)Array of T
z.tuple([A, B])Fixed positions
.nonempty()At least one element
.min / .max / .lengthSize constraints
z.recordString-key map of values
z.map / z.setES Map / Set

💡 Examples

Array of objects:

const Tag = z.string().min(1);
const Post = z.object({
  title: z.string(),
  tags: z.array(Tag).max(10),
});

Tuple:

const Pair = z.tuple([z.string(), z.number()]);
Pair.parse(["age", 30]);

Nonempty:

z.array(z.number()).nonempty().parse([1]);

⚠️ Pitfalls

  • Tuples reject extra elements unless you configure rest (version-dependent).
  • Sparse arrays and holes are unusual — prefer dense JSON arrays.
  • Very large arrays: validation is O(n); validate early at the boundary.

On this page