Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zod · Module

z.set

z.set(element) creates a Zod schema for native JavaScript Set values, validating each member with the supplied element schema. Set schemas support cardinality constraints such as nonempty, min, max, and size.

Since
4.0

How it works

Call z.set with an element schema to validate native JavaScript Set values. The resulting schema checks every member against that schema and can impose collection-size constraints such as nonempty, min, max, or exact size.

Syntax

z.set(element)

Parameters

NameTypeDescription
elementZod schemaZod schema used to validate every member of the Set.

Returns

A Zod schema representing a native Set whose members conform to the supplied element schema.

Key points

  • Pass the schema for each member to z.set; this example creates a Set schema containing numbers.
  • Type inference maps a numeric Set schema to the TypeScript type Set<number>.
  • Values validated by the schema are native JavaScript Set instances.
  • Use parse to validate a Set and obtain the parsed value when validation succeeds.
  • nonempty requires the Set to contain one or more members.
  • min(5) requires at least five members.
  • readonly changes the inferred TypeScript result to ReadonlySet<string>.

Common mistakes

  • The element schema and the Set-level size constraints are separate: use methods such as min, max, size, or nonempty for cardinality rules.

Example: Validate a non-empty string Set

import { z } from "zod";

const UserIds = z.set(z.string()).min(1);

const result = UserIds.safeParse(new Set(["u1", "u2"]));
if (result.success) {
  console.log(result.data); // Set<string>
}

Creates a non-empty set schema whose elements must be strings, then safely validates a native Set.

When to use it

Typed unique collections

Validate collections of identifiers, tags, or other unique primitive values at an API boundary.

Cardinality validation

Enforce minimum, maximum, or exact membership counts for a Set.

Readonly Set types

Expose an immutable inferred collection type after validation.

Interview questions

QWhat does z.set do?

z.set(elementSchema) creates a schema for a native Set whose members are validated by elementSchema. For example, z.set(z.number()) infers Set<number> and accepts a Set containing numbers.

Follow-up: How would you require at least five members? Use z.set(elementSchema).min(5), which applies a lower bound to the Set size.

QHow do you validate the members of a Set?

The element schema determines the type each Set member must satisfy, while the Set schema can additionally constrain cardinality. A string Set can therefore be modeled with z.set(z.string()).

Follow-up: How do you require exactly five members? Add .size(5) to the Set schema.

QWhich size constraints are available for Set schemas?

Zod provides nonempty, min, max, and size for Set cardinality. These express at least one member, minimum size, maximum size, or an exact size respectively.

Follow-up: What TypeScript type does readonly produce here? z.set(z.string()).readonly() produces ReadonlySet<string>.

  • z.map
  • z.array
  • z.infer
  • nonempty
  • min
  • max
  • size
  • readonly

Source: Defining schemas | Zod. Checked 2026-10-15.