new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle · PostgreSQL · all subjects

zod

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

createSelectSchema generates Zod schema from pgTable for query validation

createSelectSchema creates a Zod schema from a Drizzle pgTable that matches the shape of data returned by database queries. This schema can validate API responses. The schema enforces that all columns defined in the table are present in the parsed data, causing validation to fail if columns are missing from the query result.

createInsertSchema generates Zod schema from pgTable for insert validation

createInsertSchema creates a Zod schema from a Drizzle pgTable that represents the shape of data to be inserted. This schema enforces that all non-optional columns are provided. For example, a table with id (generatedAlwaysAsIdentity), name (notNull), and age (notNull) requires name and age but not id.

createUpdateSchema generates Zod schema from pgTable for update validation

createUpdateSchema creates a Zod schema from a Drizzle pgTable that represents the shape of data to be updated. All fields become optional, allowing partial updates. For example, updating users table can include any combination of fields like { age: 35 } without requiring all fields.

Schema creation functions support refinements for field customization

createSelectSchema, createInsertSchema, and createUpdateSchema accept an optional second parameter for customizing individual fields. Pass a callback function to extend or modify a field's existing schema, or pass a Zod schema directly to completely overwrite the field's schema including its nullability.

createSchemaFactory allows using extended Zod instances

createSchemaFactory accepts a configuration object with a zodInstance property, allowing schema creation with custom or extended Zod instances like @hono/zod-openapi. This enables using extended Zod features when creating schemas.

createSchemaFactory supports type coercion configuration

createSchemaFactory accepts a coerce configuration object to enable automatic type coercion. Set coerce.date to true to coerce dates, or set coerce to true to coerce all data types. For example, timestamp columns can be coerced to z.coerce.date().

pgEnum and pgView support schema generation with createSelectSchema

createSelectSchema works with pgEnum to generate z.enum schema and with pgView to generate schemas matching the view's structure. Views follow the same schema generation as tables.

Drizzle pg-core to Zod type mappings

Drizzle pg-core column types map to Zod schemas as follows: boolean -> z.boolean(); pgEnum -> z.enum(); date/timestamp with mode 'date' -> z.date(); cidr, inet, interval, macaddr, macaddr8, numeric, text, sparsevec, time -> z.string(); bit -> z.string().regex(/^[01]+$/).max(dimensions); uuid -> z.string().uuid(); char -> z.string().length(length); varchar -> z.string().max(length); text/char/varchar with enum -> z.enum(enum); smallint/smallserial -> z.number().min(-32768).max(32767).int(); real -> z.number().min(-8388608).max(8388607); integer/serial -> z.number().min(-2147483648).max(2147483647).int(); doublePrecision -> z.number().min(-140737488355328).max(140737488355327); bigint/bigserial with mode 'number' -> z.number().min(-9007199254740991).max(9007199254740991).int(); bigint/bigserial with mode 'bigint' -> z.bigint().min(-9223372036854775808n).max(9223372036854775807n); geometry/point with mode 'tuple' -> z.tuple([z.number(), z.number()]); geometry/point with mode 'xy' -> z.object({ x: z.number(), y: z.number() }); halfvec/vector -> z.array(z.number()).length(dimensions); line with mode 'abc' -> z.object({ a: z.number(), b: z.number(), c: z.number() }); line with mode 'tuple' -> z.tuple([z.number(), z.number(), z.number()]); json/jsonb -> z.union([z.union([z.string(), z.number(), z.boolean(), z.null()]), z.record(z.any()), z.array(z.any())]); dataType.array -> z.array(baseDataTypeSchema).length(size); bytea -> zod.custom<Buffer>((v) => v instanceof Buffer).

Install drizzle-orm and zod for Zod schema generation

To use Zod schema generation with Drizzle, install drizzle-orm@rc and zod.

Give your agent this brain