egon-orm-workspace / @egon-orm/zod / toZod
Function: toZod() ​
function toZod<S, M, P, O>(schema, options?): ZodObject<ToZodShape<S, M, Exclude<P & ToZodKeys<S, M>, O>>>;Defined in: zod/src/to-zod.ts:315
Derives a Zod object schema from a schema class: one key per field, with the field's type and nullability, so the shape is written once. Virtual and redacted fields are included in "row" mode; "insert" leaves virtual fields out and makes a field optional where an insert does not need it; "update" makes every field optional.
The result is an ordinary Zod object: .extend() it to add rules (z.string().email()), and pass it to cs.validate().
Type Parameters ​
S ​
S extends AnySchema
M ​
M extends ToZodMode = "row"
P ​
P extends string = ToZodKeys<S, M>
O ​
O extends string = never
Parameters ​
schema ​
S
options? ​
ToZodOptions<S, M, P, O>
Returns ​
ZodObject<ToZodShape<S, M, Exclude<P & ToZodKeys<S, M>, O>>>
Example ​
const UserZ = toZod(User); // matches the fields of a User row
const CreateUser = toZod(User, { mode: "insert", pick: ["email", "name", "age"] })
.extend({ email: z.string().email() });
type CreateUser = z.infer<typeof CreateUser>; // { email: string; name?: string | null; age?: number | null }Throws ​
QueryError when schema is not a schema class, mode is unknown, or pick or omit names something that is not a field of the mode.

