drizzle-zod
If you know SQL, you know Drizzle ORM
drizzle-zod
is a plugin for Drizzle ORM that allows you to generate Zod schemas from Drizzle ORM schemas.
Features
- Create a select schema for tables, views and enums.
- Create insert and update schemas for tables.
- Supports all dialects: PostgreSQL, MySQL and SQLite.
Usage
import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { z } from 'zod';
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const insertUserSchema = createInsertSchema(users);
const updateUserSchema = createUpdateSchema(users);
const selectUserSchema = createSelectSchema(users);
const insertUserSchema = createInsertSchema(users, {
role: z.string(),
});
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.positive(),
email: (schema) => schema.email(),
role: z.string(),
});
const user = insertUserSchema.parse({
name: 'John Doe',
email: 'johndoe@test.com',
role: 'admin',
});
const requestSchema = insertUserSchema.pick({ name: true, email: true });