Registry / database / drizzle-zod

drizzle-zod

JSON →
library0.8.3jsnpmunverified

Generate Zod validation schemas from Drizzle ORM table definitions. Current stable version: 0.8.3, released alongside Drizzle ORM updates. Provides seamless integration with drizzle-orm and zod, allowing developers to define Zod schemas that mirror their database schema, reducing duplication and ensuring consistency. Differentiators: automatic generation of Zod schemas with proper types, support for custom filters and mutations, tight coupling with Drizzle ORM's table schema. Peer dependencies: zod ^3.25.0 || ^4.0.0, drizzle-orm >=0.36.0. Release cadence: tends to follow Drizzle ORM updates, with minor versions for feature additions and patches for fixes.

npm install drizzle-zod
INSTALL
IMPORT
SIG · DRIZZLE-ZOD
D
drizzle-zod
databasejavascriptv0.8.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createInsertSchema
import { createInsertSchema } from 'drizzle-zod'
const createInsertSchema = require('drizzle-zod')
ESM-only package; requires module type or .mjs extension. Use import statements.
createSelectSchema
import { createSelectSchema } from 'drizzle-zod'
import { createSelectSchema } from 'drizzle-zod/schemas'
All functions are exported from the main package. No subpath exports exist.
createUpdateSchema
import { createUpdateSchema } from 'drizzle-zod'
import { CreateUpdateSchema } from 'drizzle-zod'
Function name is camelCase, not PascalCase.
z
import { z } from 'zod'
import { z } from 'drizzle-zod'
z is from zod, not from drizzle-zod. Common mistake is to import from wrong package.

Demonstrates creating insert and select Zod schemas from Drizzle SQLite table definition, then validates data with safeParse. Also shows custom field validations and coercion.

import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'; import { createInsertSchema, createSelectSchema } from 'drizzle-zod'; import { z } from 'zod'; const users = sqliteTable('users', { id: integer('id').primaryKey(), name: text('name').notNull(), age: integer('age'), }); const insertUserSchema = createInsertSchema(users); const selectUserSchema = createSelectSchema(users); // Example: validate an insert object const input = { name: 'Alice', age: 30 }; const result = insertUserSchema.safeParse(input); if (result.success) { console.log('Valid data:', result.data); } else { console.error('Validation error:', result.error.issues); } // Custom validation: add refinements const customSchema = createInsertSchema(users, { name: z.string().min(2, 'Name must be at least 2 characters'), }); // Custom validation: add refinements with coerce const schemaWithCoerce = createInsertSchema(users, { age: z.coerce.number().int().positive(), });
Debug
Known issues
breakingPackage was ESM-only since version 0.8.0. CommonJS require() will throw.
fix
Use import syntax or set package.json "type": "module". Alternatively, use dynamic import: const { createInsertSchema } = await import('drizzle-zod');
affects: >=0.8.0
deprecatedcreateUpdateSchema was added in 0.7.0, but previous versions used a different pattern; ensure you use correct version.
fix
Use createUpdateSchema from drizzle-zod directly; avoid custom implementations.
affects: >=0.7.0
gotchacreateInsertSchema generates a schema that requires all fields that are not nullable or have defaults. This may be stricter than your database constraints.
fix
Use the second argument to override field types: createInsertSchema(table, { field: z.string().optional() })
affects: all
gotchaWhen using with PostgreSQL, types like jsonb, array, etc. may produce complex Zod schemas that need manual adjustment.
fix
Check generated schema and apply overrides for those fields using the second parameter.
affects: all
gotchaTypeScript: You might need to use `z.infer<typeof insertUserSchema>` to get the type of the validated output.
fix
Use z.infer to extract the type. Example: type InsertUser = z.infer<typeof insertUserSchema>;
affects: all
Errors
Common errors & fixes
TypeError: drizzle_zod.createInsertSchema is not a function
Package is ESM-only, but you are using CommonJS require().
fix
Change to import statement: import { createInsertSchema } from 'drizzle-zod'; or use dynamic import in CJS.
Cannot find module 'drizzle-zod' or its corresponding type declarations.
Package is not installed or NPM resolution issue.
fix
Run 'npm install drizzle-zod' and ensure it's in dependencies. Also install peer deps: 'npm install zod drizzle-orm'.
Type 'undefined' is not assignable to type 'string' (or similar Zod validation error)
createInsertSchema marks non-nullable fields as required. If a field has a default in DB but is not marked nullable, Zod expects it on insert.
fix
Override the field schema: createInsertSchema(table, { optionalField: z.string().optional() })
ZodError: Expected number, received string (for numeric fields)
Drizzle schema may define field as integer, but input from form is string. Zod schema expects number.
fix
Use z.coerce.number() on the field: createInsertSchema(table, { age: z.coerce.number() })
Upgrade
Version history
0.8.3latest on npm
Audit
Dependencies
zodrequiredruntime dependency for validation
drizzle-ormrequiredpeer dependency required to resolve Drizzle table schema types
Agent activity
12 hits · last 30 days
node
10
Resources
drizzle-zod — npm install drizzle-zod · libregistry