Skip to main content

Basic Usage

Create a schema for JavaScript Map objects with validated keys and values.

Signature

ZodType
required
Schema to validate all map keys.
ZodType
required
Schema to validate all map values.
string | ZodMapParams
Optional error message (string) or configuration object.

Properties

ZodType
Access the key schema.
ZodType
Access the value schema.

Methods

min()

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

max()

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

size()

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

nonempty()

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

Complex Types

Object Values

Object Keys

When using object keys, remember that JavaScript uses reference equality. Two objects with the same shape are different keys.

Nested Maps

Type Inference

Common Patterns

Cache Implementation

Entity Store

Adjacency List (Graph)

Refinements

Add custom validation:

Map vs Record

Transformations

Transform map data: