This version fully updates drizzle-zod
integration and makes sure it's compatible with newer zod versions
You must also have Drizzle ORM v0.38.0 or greater and Zod v3.0.0 or greater installed.
- When refining a field, if a schema is provided instead of a callback function, it will ignore the field's nullability and optional status.
- Some data types have more specific schemas for improved validation
Thanks to @L-Mario564 for making more updates than we expected to be shipped in this release. We'll copy his message from a PR regarding improvements made in this release:
- Output for all packages are now unminified, makes exploring the compiled code easier when published to npm.
- Smaller footprint. Previously, we imported the column types at runtime for each dialect, meaning that for example, if you're just using Postgres then you'd likely only have drizzle-orm and drizzle-orm/pg-core in the build output of your app; however, these packages imported all dialects which could lead to mysql-core and sqlite-core being bundled as well even if they're unused in your app. This is now fixed.
- Slight performance gain. To determine the column data type we used the is function which performs a few checks to ensure the column data type matches. This was slow, as these checks would pile up every quickly when comparing all data types for many fields in a table/view. The easier and faster alternative is to simply go off of the column's columnType property.
createSelectSchema
function now also accepts views and enums.
import { pgEnum } from 'drizzle-orm/pg-core';
import { createSelectSchema } from 'drizzle-zod';
const roles = pgEnum('roles', ['admin', 'basic']);
const rolesSchema = createSelectSchema(roles);
const parsed: 'admin' | 'basic' = rolesSchema.parse(...);
const usersView = pgView('users_view').as((qb) => qb.select().from(users).where(gt(users.age, 18)));
const usersViewSchema = createSelectSchema(usersView);
const parsed: { id: number; name: string; age: number } = usersViewSchema.parse(...);
- New function:
createUpdateSchema
, for use in updating queries.
import { pgTable, text, integer } from 'drizzle-orm/pg-core';
import { createUpdateSchema } from 'drizzle-zod';
const users = pgTable('users', {
id: integer().generatedAlwaysAsIdentity().primaryKey(),
name: text().notNull(),
age: integer().notNull()
});
const userUpdateSchema = createUpdateSchema(users);
const user = { id: 5, name: 'John' };
const parsed: { name?: string | undefined, age?: number | undefined } = userUpdateSchema.parse(user); // Error: `id` is a generated column, it can't be updated
const user = { age: 35 };
const parsed: { name?: string | undefined, age?: number | undefined } = userUpdateSchema.parse(user); // Will parse successfully
await db.update(users).set(parsed).where(eq(users.name, 'Jane'));
- New function:
createSchemaFactory
, to provide more advanced options and to avoid bloating the parameters of the other schema functions
import { pgTable, text, integer } from 'drizzle-orm/pg-core';
import { createSchemaFactory } from 'drizzle-zod';
import { z } from '@hono/zod-openapi'; // Extended Zod instance
const users = pgTable('users', {
id: integer().generatedAlwaysAsIdentity().primaryKey(),
name: text().notNull(),
age: integer().notNull()
});
const { createInsertSchema } = createSchemaFactory({ zodInstance: z });
const userInsertSchema = createInsertSchema(users, {
// We can now use the extended instance
name: (schema) => schema.openapi({ example: 'John' })
});
- Full support for PG arrays
pg.dataType().array(...);
// Schema
z.array(baseDataTypeSchema).length(size);