Function: zodSchema()
zodSchema<
T>(table):TableSchema<T>
Return a Zod schema for the table.
Type Parameters
| Type Parameter |
|---|
T extends PgTable<any, any> |
Parameters
| Parameter | Type |
|---|---|
table | T |
Returns
TableSchema<T>
Remarks
The schema will be for all columns defined in the table. You can use extend, pick, omit, or shape to adapt/expand the schema.
Schema validations for all columns
- Input and output values are optional by default.
- Input and output types will be automatically inferred.
- Explicit
undefinedvalues will result in an error. - Each column type has extended validation rules to allow only accepted values for the column type. Refer to each columm documentation for a description on the specific validation rules. For example, the schema for an
integercolumn:- Will not allow values lower than -2147483648.
- Will not allow values greater that 2147483647.
- The schema will take into account account constraints, generated values and default data values. For example, a non-nullable, primary key column:
- Can't be null.
- Input value and output values are required.
Schema Types
Each column has a TypeScript type for input and output values (parsed) in the schema. The output values match the select type for the column(except bytea columns *)
(*) Since Buffer is a Node.js API, the schema will not coerce the input to Buffer for browser compatibility. The output type will be the same as the input type.
| Column | Input | Output |
|---|---|---|
| bigint | bigint | number | string | string |
| bigserial | bigint | number | string | string |
| bit | string | string |
| bitVarying | string | string |
| boolean | boolean | Boolish* | boolean |
| bytea | Buffer | string | Buffer | string |
| characterVarying | string | string |
| character | string | string |
| cidr | string | string |
| date | Date | string | Date |
| doublePrecision | bigint | number | string | string |
| enumerated | enum values | enum values |
| inet | string | string |
| integer | number | string | number |
| json | JsonValue* | JsonValue* |
| jsonb | JsonValue* | JsonValue* |
| macaddr | string | string |
| macaddr8 | string | string |
| numeric | bigint | number | string | number |
| real | bigint | number | string | string |
| serial | number | string | number |
| smallint | number | string | number |
| time | string | string |
| timeWithTimeZone | string | string |
| timestamp | Date | string | Date |
| timestampWithTimeZone | Date | string | Date |
| tsquery | string | string |
| tsvector | string | string |
| uuid | string | string |
| xml | string | string |
(*) Boolish and JsonValue are defined as follows:
ts
type Boolish = "true" | "false" | "yes" | "no" | 1 | 0 | "1" | "0" | "on" | "off";
type JsonArray = JsonValue[];
type JsonValue = boolean | number | string | Record<string, unknown> | JsonArray;Example
ts
const userRole = enumType("user_role", ["admin", "user"]);
const users = table({
columns: {
id: integer().generatedAlwaysAsIdentity(),
name: text(),
role: enumerated(userRole).notNull(),
orderCount: integer().notNull().default(0),
createdAt: timestampWithTimeZone().default(sql`now`).notNull(),
},
constraints: {
primaryKey: primaryKey(["id"]),
},
});
const schema = zodSchema(users);
type InputType = z.input<typeof schema>;
type OutputType = z.output<typeof schema>;InputType will be:
ts
type InputType = {
id: never;
name?: string | null | undefined;
role: "user" | "admin";
orderCount: number | string | undefined;
createdAt?: Date | string | undefined;
}OutputType will be:
ts
type OutputType = {
id: never;
name?: string | null | undefined;
orderCount?: number | undefined;
role: "user" | "admin";
createdAt?: Date | undefined;
}