Skip to main content

Basic Usage

Create a schema for objects with dynamic keys, where all values share the same type.

Signature

ZodString | ZodNumber | ZodEnum
required
Schema to validate keys. Can be z.string(), z.number(), or z.enum().
ZodType
required
Schema to validate all values.
string | ZodRecordParams
Optional error message (string) or configuration object.

Properties

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

Key Types

String Keys

Most common use case:

Number Keys

For numeric indices:
In JavaScript, numeric keys are automatically converted to strings. The schema accepts both { 0: 'value' } and { '0': 'value' }.

Enum Keys

Restrict keys to specific values:

Complex Value Types

Object Values

Array Values

Union Values

Partial Records

Create records where not all keys are required:
Signature:

Loose Records

Create records that allow unrecognized keys:
Signature:

Type Inference

Common Patterns

Configuration Objects

Translation Dictionaries

Counters and Metrics

Entity Stores

Refinements

Add custom validation:

Record vs Object

Transformations

Transform record values: