JSPM

drizzle-zod

0.1.3-20c5fdb
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 723846
  • Score
    100M100P100Q194199F
  • License Apache-2.0

Generate Zod schemas from Drizzle ORM schemas

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (drizzle-zod) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    drizzle-zod npm

    npm zod version npm bundle size Discord License
    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.

    Database Insert schema Select schema
    PostgreSQL
    MySQL
    SQLite

    Usage

    import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
    import { z } from 'zod';
    import { createInsertSchema } from 'drizzle-zod/pg';
    
    const users = pgTable('users', {
      id: serial('id').primaryKey(),
      name: text('name').notNull(),
      email: text('email').notNull(),
      role: text<'admin' | 'user'>('role').notNull(),
      createdAt: timestamp('created_at').notNull().defaultNow(),
    });
    
    const newUserSchema = createInsertSchema(users);
    
    // Transform keys to snake case
    const newUserSchema = createInsertSchema(users, 'snake');
    
    // Transform keys to camel case
    const newUserSchema = createInsertSchema(users, 'camel');
    
    // Override the fields
    const newUserSchema = createInsertSchema(users, {
      // this is required for runtime validation of text enum types, otherwise z.string() will be used
      role: z.enum(['admin', 'user']),
    });
    
    // Refine the fields
    const newUserSchema = createInsertSchema(users, (schema) => ({
      id: schema.id.positive(),
      email: schema.email.email(),
      role: z.enum(['admin', 'user']),
    }));
    
    const newUserSchema = createInsertSchema(users, 'snake', (schema) => ({
      created_at: schema.createdAt.min(new Date()),
    }));
    
    // Usage
    
    const user = newUserSchema.parse({
      name: 'John Doe',
      email: 'johndoe@test.com',
      role: 'admin',
    });
    
    // Zod schema type is also inferred from the table schema, so you have full type safety
    const requestSchema = newUserSchema.pick({ name: true, email: true });