Skip to Content
Database

Zod

Zod schemas generated from a table's columns, for request bodies and rows.

Usage

import { toZod } from "@dapplelight/signet-db/zod"
import { app } from "./app"
import { invoices } from "./schema"

const createInvoice = toZod(invoices, { mode: "insert" })
const patchInvoice = toZod(invoices, { mode: "update" })

app.post("/invoices", async (c) => {
  const body = createInvoice.parse(await c.req.json())
})

API

ExportWhat it does
toZod(table, { mode, keep? })A Zod object schema for the table. Unknown keys are stripped.
MANAGED_KEYSid, createdAt, updatedAt, deletedAt, userId: left out of insert and update.
ZodMode"insert" | "update" | "select".
ZodOutput<T, M, K>The TypeScript type a mode produces.

toZod(table, options)

OptionTypeDefault
mode"insert" | "update" | "select"requiredWhat the schema validates.
keepmanaged keys[]Managed keys to keep in insert and update.
ModeSchema
insertWhat a client may send. Columns with a default or that allow null are optional.
updateThe insert schema with every field optional.
selectA full row as read through signet-db (encrypted fields as plaintext or null).

Rules the schema applies:

  • Dates accept ISO strings and epoch milliseconds.
  • A column named <x>Currency next to <x> must be three capital letters.
  • Integer columns must be safe integers.
  • Encrypted columns take plaintext strings.
  • A primary key id is left out only when it has a default.

Examples

Let an import supply the id

import { toZod } from "@dapplelight/signet-db/zod"
import { invoices } from "./schema"

const importInvoice = toZod(invoices, { mode: "insert", keep: ["id"] })