dynotable-orm/zod API: the Zod 4 adapter

What does dynotable-orm/zod add to Zod?

dynotable-orm/zod attaches DynamoDB metadata to Zod 4 fields: the hash and range key, indexes, combined fields and Date storage. Use the functions, such as hashKey(z.string()), or call extendZod(z) once to get the same marks as methods, such as z.string().hashKey(). Each mark returns a new schema and never changes the one you pass.

import {model} from 'dynotable-orm';
import {combine, getDynamoMeta, hashKey, index, rangeKey, storage} from 'dynotable-orm/zod';
import {z} from 'zod';

const device = index(z.string(), {name: 'byDevice', rangeKey: 'startedAt'});

export const Session = model(
  'Session',
  z.object({
    userId: hashKey(z.string()),
    startedAt: rangeKey(z.number()),
    device,
    expiresAt: storage(z.date(), 'seconds'),
    deviceUser: combine(z.string(), ['device', 'userId'], '#')
  })
);

console.log(getDynamoMeta(device)); // {index: [{name: 'byDevice', rangeKey: 'startedAt'}]}

A setting that a field and a model option both give, with different values, throws a ConfigError at model().

extendZod()

function extendZod(z): void;

Adds .hashKey(), .rangeKey(), .index(), .combine() and .storage() to Zod schemas, the method form of this module's functions. Only schemas created after the call have them.

Parameters

ParameterType
z{ ZodType: { prototype: object; }; }
z.ZodType{ prototype: object; }
z.ZodType.prototypeobject

Returns

void

hashKey()

function hashKey<S>(s): WithMeta<
  S,
  {
    hashKey: true;
  }
>;

Type Parameters

Type Parameter
S extends Schema

Parameters

ParameterType
sS

Returns

WithMeta<S, { hashKey: true; }>

rangeKey()

function rangeKey<S>(s): WithMeta<
  S,
  {
    rangeKey: true;
  }
>;

Type Parameters

Type Parameter
S extends Schema

Parameters

ParameterType
sS

Returns

WithMeta<S, { rangeKey: true; }>

index()

function index<S, D>(
  s,
  def?
): WithMeta<
  S,
  {
    index: D;
  }
>;

Type Parameters

Type ParameterDefault type
S extends Schema-
D extends | IndexDefinition<string> | readonly IndexDefinition<string>[]object

Parameters

ParameterType
sS
def?D

Returns

WithMeta<S, { index: D; }>

combine()

function combine<S, F>(
  s,
  sources,
  separator?
): WithMeta<
  S,
  {
    combine: F;
  }
>;

Type Parameters

Type Parameter
S extends $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> & object & StandardSchema<unknown, string>
F extends readonly string[]

Parameters

ParameterType
sS
sourcesF
separator?string

Returns

WithMeta<S, { combine: F; }>

storage()

function storage<S, D>(
  s,
  mode
): WithMeta<
  S,
  {
    storage: D;
  }
>;

Type Parameters

Type Parameter
S extends $ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> & object & StandardSchema<unknown, Date | null | undefined>
D extends DateStorage

Parameters

ParameterType
sS
modeD

Returns

WithMeta<S, { storage: D; }>

getDynamoMeta()

function getDynamoMeta(schema): FieldMeta | undefined;

The metadata of a field schema: its own (kept by .min(), .describe(), def copies…), else its inner schema's. Typed structurally: this module is shared with the main entry, whose declarations must not import Zod.

Parameters

ParameterType
schema{ _zod: { def: object; }; }
schema._zod{ def: object; }
schema._zod.defobject

Returns

FieldMeta | undefined

FieldMeta type

Metadata attached to one field.

Properties

PropertyType
combine?CombineDefinition<string>
hashKey?boolean
index?readonly IndexDefinition<string>[]
rangeKey?boolean
storage?DateStorage

Updated