Dynamic string-keyed maps
Validate caches, lookup tables, or stores indexed by arbitrary string IDs.
Zod · Function
Creates a schema for dictionary-like objects with dynamic keys. It validates each key with one schema and each value with another, producing a corresponding TypeScript Record type.
z.record(keySchema, valueSchema) creates a schema for an object with dynamically named keys. Each key is checked against the key schema and each corresponding value against the value schema; the inferred TypeScript type is a Record with those key and value types.
z.record(keySchema, valueSchema)| Name | Type | Description |
|---|---|---|
keySchema | Zod schema | Schema applied to each property key; it must be assignable to string | number | symbol. |
valueSchema | Zod schema | Schema applied to each property value. |
A Zod record schema whose inferred output has the shape Record<Key, Value>.
z.looseRecord() when non-matching keys should pass through unchanged.import { z } from "zod";
const UserStore = z.record(z.string(), z.object({ name: z.string() }));
const result = UserStore.parse({
alice: { name: "Alice" },
bob: { name: "Bob" },
});
console.log(result.alice.name);This validates an object whose keys are strings and whose values are objects containing a string name.
Validate caches, lookup tables, or stores indexed by arbitrary string IDs.
Require a consistent structure for every value in a keyed collection.
Represent a finite, exhaustive set of named entries with an enum key schema.
z.record validate?z.record(keySchema, valueSchema) validates a dictionary-like object with dynamic keys. The key schema must be assignable to string | number | symbol, while the value schema validates every property value.
Follow-up: How would you model a finite set of required keys? Use an enum as the key schema; current Zod checks exhaustiveness and requires all enum keys during parsing.
For z.record(z.string(), z.number()), each property key must satisfy the string key schema and each property value must be a number. Its inferred type corresponds to Record<string, number>.
Follow-up: What happens with an invalid key? The default record schema reports an error; z.looseRecord instead preserves keys that do not match.
The current signature takes two schemas: one for keys and one for values. The older single-argument form is no longer supported in the current major version.
Follow-up: Can the key schema be numeric? Yes; Zod validates an object key as a valid numeric string when a number schema is used.
z.looseRecordz.objectz.enumz.infer