Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

drizzle-zod

Package Overview
Dependencies
Maintainers
1
Versions
891
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

drizzle-zod

Generate Zod schemas from Drizzle ORM schemas

  • 0.1.1-1e62a3e
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
153K
decreased by-4.9%
Maintainers
1
Weekly downloads
 
Created
Source

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.

DatabaseInsert schemaSelect schema
PostgreSQL
MySQL
SQLite

Usage

import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm-pg';
import { z } from 'zod';
import { createInsertSchema } from 'drizzle-zod';

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 });

Keywords

FAQs

Package last updated on 16 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc