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
| Parameter | Type |
|---|---|
z | { ZodType: { prototype: object; }; } |
z.ZodType | { prototype: object; } |
z.ZodType.prototype | object |
Returns
void
hashKey()
function hashKey<S>(s): WithMeta<
S,
{
hashKey: true;
}
>;Type Parameters
| Type Parameter |
|---|
S extends Schema |
Parameters
| Parameter | Type |
|---|---|
s | S |
Returns
WithMeta<S, {
hashKey: true;
}>
rangeKey()
function rangeKey<S>(s): WithMeta<
S,
{
rangeKey: true;
}
>;Type Parameters
| Type Parameter |
|---|
S extends Schema |
Parameters
| Parameter | Type |
|---|---|
s | S |
Returns
WithMeta<S, {
rangeKey: true;
}>
index()
function index<S, D>(
s,
def?
): WithMeta<
S,
{
index: D;
}
>;Type Parameters
| Type Parameter | Default type |
|---|---|
S extends Schema | - |
D extends | IndexDefinition<string> | readonly IndexDefinition<string>[] | object |
Parameters
| Parameter | Type |
|---|---|
s | S |
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
| Parameter | Type |
|---|---|
s | S |
sources | F |
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
| Parameter | Type |
|---|---|
s | S |
mode | D |
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
| Parameter | Type |
|---|---|
schema | { _zod: { def: object; }; } |
schema._zod | { def: object; } |
schema._zod.def | object |
Returns
FieldMeta | undefined
FieldMeta type
Metadata attached to one field.
Properties
| Property | Type |
|---|---|
combine? | CombineDefinition<string> |
hashKey? | boolean |
index? | readonly IndexDefinition<string>[] |
rangeKey? | boolean |
storage? | DateStorage |