Skip to main content

Basic Usage

Create a schema for JavaScript Set objects with validated unique values.

Signature

ZodType
required
Schema to validate all set values.
string | ZodSetParams
Optional error message (string) or configuration object.

Uniqueness

Sets automatically ensure uniqueness:
Sets use value equality for primitives (string, number, boolean) and reference equality for objects. Two objects with identical properties are considered different values.

Methods

min()

Set a minimum size for the set.
Signature:
number
required
Minimum number of unique values required.
string | object
Optional error message or configuration object.

max()

Set a maximum size for the set.
Signature:
number
required
Maximum number of unique values allowed.
string | object
Optional error message or configuration object.

size()

Set an exact size for the set.
Signature:
number
required
Exact number of unique values required.
string | object
Optional error message or configuration object.

nonempty()

Require at least one value (equivalent to .min(1)).
Signature:
string | object
Optional error message or configuration object.

Complex Value Types

Object Values

Enum Values

Nested Sets

Type Inference

Common Patterns

Tags/Categories

Unique IDs

Permissions

Graph Nodes

Refinements

Add custom validation:

Set vs Array

Transformations

Transform set data:
  • z.array() - For ordered collections with duplicates
  • z.map() - For key-value pairs
  • z.enum() - For defining value options