xcvzmoon/sequelize-zod(新しいタブで開きます)
主要言語: TypeScript
Generate Zod schemas automatically from your Sequelize models.
- star 数
- 4
- watcher 数
- 0
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2026/01/31
主要言語: TypeScript
Generate Zod schemas automatically from your Sequelize models.
Generate Zod schemas from your Sequelize models. This library is inspired by drizzle-zod: you define attributes with a column builder, optionally register a Sequelize model, and get select, insert, and update Zod schemas with correct TypeScript inference.
pnpm add @xcvzmoon/sequelize-zod sequelize zod
Peer dependencies: sequelize (^6.37.7) and zod (^4.3.6).
integer(), string(), date(), etc.) that maps to Sequelize data types.createSelectSchema, createInsertSchema, and createUpdateSchema produce Zod schemas from either your attribute object or from an existing Sequelize model..min(), .max(), or custom validation).You can use the column builder and schema helpers without a database; only defineModel requires a Sequelize instance.
import { integer, string, boolean, date, enumType, defineModel, createSelectSchema, createInsertSchema, createUpdateSchema, } from '@xcvzmoon/sequelize-zod'; import type { InferModelFromAttributes } from '@xcvzmoon/sequelize-zod'; import { Sequelize } from 'sequelize'; import { z } from 'zod'; const userAttributes = { id: integer().primaryKey().autoIncrement(), name: string(100).notNull(), active: boolean().defaultValue(true), role: enumType('admin', 'user', 'guest').notNull().defaultValue('user'), createdAt: date().defaultValue(() => new Date()), }; type UserModel = InferModelFromAttributes<typeof userAttributes>;
const selectSchema = createSelectSchema(userAttributes); const insertSchema = createInsertSchema(userAttributes); const updateSchema = createUpdateSchema(userAttributes); type UserSelect = z.infer<typeof selectSchema>; type UserInsert = z.infer<typeof insertSchema>; type UserUpdate = z.infer<typeof updateSchema>; const result = insertSchema.safeParse({ name: 'Alice', role: 'user' });
const sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false }); const User = defineModel(sequelize, 'User', userAttributes); const selectFromModel = createSelectSchema(User); const insertFromModel = createInsertSchema(User); const updateFromModel = createUpdateSchema(User);
Columns are defined with builder functions. Each returns a chainable builder with methods such as .primaryKey(), .autoIncrement(), .notNull(), .allowNull(), .defaultValue(), .unique(), .references(), etc.
| Function | Sequelize type | Notes |
|---|---|---|
integer() |
INTEGER | |
bigint() |
BIGINT | TypeScript: bigint |
float(l?, d?) |
FLOAT | Optional length/decimals |
real(l?, d?) |
REAL | |
double(l?, d?) |
DOUBLE | |
decimal(p?, s?) |
DECIMAL | Optional precision/scale |
| Function | Sequelize type | Notes |
|---|---|---|
string(n?) |
STRING | Optional length |
char(n?) |
CHAR | |
text(t?) |
TEXT | 'tiny' | 'medium' | 'long' |
| Function | Sequelize type | Notes |
|---|---|---|
boolean() |
BOOLEAN | |
date(n?) |
DATE | JavaScript Date |
dateonly() |
DATEONLY | |
time() |
TIME |
| Function | Sequelize type |
|---|---|
uuid() |
UUID |
uuidv1() |
UUIDV1 |
uuidv4() |
UUIDV4 |
| Function | Sequelize type | Notes |
|---|---|---|
json<T>() |
JSON | Generic for shape |
jsonb<T>() |
JSONB | |
blob(t?) |
BLOB | 'tiny' | 'medium' | 'long' |
| Function | Sequelize type | Notes |
|---|---|---|
enumType(...vals) |
ENUM | Literal union |
array(type) |
ARRAY | Pass Sequelize DataTypes |
range(), geometry(), geography(), hstore(), cidr(), inet(), macaddr(), citext(), tsvector(). Virtual columns: virtual(returnType, dependencies?).
Builds a Zod object schema for the full row shape (e.g. query results). All columns are included; optionality follows allowNull and hasDefault.
userAttributes) or a Sequelize ModelStatic.Record<string, z.ZodType | (schema => z.ZodType)> to override or refine fields.Builds a Zod schema for insert payloads. Auto-generated (identity) columns are omitted. Fields are optional when allowNull or hasDefault is true.
createSelectSchema.Builds a Zod schema for partial updates. All fields are optional; generated columns are omitted.
createSelectSchema.const insertSchema = createInsertSchema(userAttributes, { name: z.string().min(2).max(100), role: (schema) => schema.refine((v) => ['admin', 'user', 'guest'].includes(v as string)), });
Registers a Sequelize model with the given name and the attribute record built with the column factory. Returns a ModelStatic whose instance type matches InferModelFromAttributes<typeof attributes>.
Use the same attributes with createSelectSchema, createInsertSchema, and createUpdateSchema so types stay in sync.
| Type | Description |
|---|---|
InferModelFromAttributes<T> |
Sequelize model instance type from an attribute record T. |
InferSelectFromAttributes<T> |
Select/row shape from attributes. Use with z.infer<typeof createSelectSchema(attrs)>. |
InferSelectFromModel<M> |
Select shape from a Sequelize model. |
InferInsertFromAttributes<T> |
Insert payload type from attributes. |
InferInsertFromModel<M> |
Insert payload type from a model. |
InferUpdateFromAttributes<T> |
Partial update type from attributes. |
InferUpdateFromModel<M> |
Partial update type from a model. |
ColumnSchemaDescriptor |
Internal descriptor (category, allowNull, hasDefault, isGenerated, enumValues). |
DataTypeCategory |
Category string used for Zod mapping ('integer', 'string', etc.). |
This project is inspired by drizzle-zod, which provides Zod schema generation from Drizzle ORM schemas. Sequelize Zod aims to offer a similar workflow for Sequelize: define columns once, then derive select, insert, and update schemas with full type inference.
ISC