Skip to content

zodCodec

zodCodec<T>(schema, name?): Codec<T>

Defined in: src/persistence/migration/Codec.ts:105

Codec that validates with any parse-style schema (Zod, valibot, hand-rolled). We don’t import zod directly — the user’s project owns the dependency and passes the schema in.

import { z } from ‘zod’;

const Deposited = z.object({ kind: z.literal(‘deposited’), amount: z.number().int().nonnegative(), currency: z.enum([‘USD’, ‘EUR’]), });

const adapter = validatedEventAdapter( defaultsAdapter({ … }), zodCodec(Deposited), );

Validates:

  • On toJournal: a programmer-bug malformed event is caught at write time, not after it’s already on disk.
  • On fromJournal: a corrupted / hand-edited journal record fails the deserialise instead of silently producing garbage.

zodCodec does NOT participate in schema-evolution by itself — pair it with defaultsAdapter, migratingAdapter, or the SchemaRegistry to cover version differences. This codec validates one specific shape; it has no idea what older or newer versions look like.

T

ParserLike<T>

string = 'zod'

Codec<T>