Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,456 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prisma Zod Generator

A Prisma generator that emits Zod schemas from your Prisma schema. It runs as part of prisma generate, so your validation schemas are regenerated from the same source of truth as your Prisma Client.

npm version downloads CI license MIT

Documentation · PZG Pro

Requirements

Dependency Supported
Node.js >= 20.19.0 (22.x recommended)
Prisma 7.x
Zod >= 3.25.0 < 5 (both v3 and v4 output are emitted)
TypeScript >= 5.4 (5.9.x recommended)

Install

npm install --save-dev prisma-zod-generator
npm install zod

Install the generator locally rather than globally: Prisma resolves the generator executable from node_modules/.bin, so a project-local install is what makes provider = "prisma-zod-generator" work. zod is a peer dependency (>=3.25.0 <5) because the generated files import it at runtime.

Usage

Add the generator to schema.prisma:

generator client {
  provider = "prisma-client"
  output   = "../src/generated/prisma"
}

generator zod {
  provider = "prisma-zod-generator"
  // output defaults to <schema dir>/generated
  // config = "./zod-generator.config.json"
}

Then run:

npx prisma generate

With no configuration you get one schema file per Prisma CRUD operation, plus the input object and enum schemas they reference:

prisma/generated/schemas/
├── findManyUser.schema.ts     # UserFindManySchema, UserFindManyZodSchema
├── createOneUser.schema.ts    # UserCreateOneSchema, UserCreateOneZodSchema
├── objects/                   # UserWhereInputObjectSchema, UserCreateInputObjectSchema, ...
├── enums/                     # SortOrderSchema, UserScalarFieldEnumSchema, ...
├── results/                   # UserFindManyResultSchema, ...
├── variants/                  # pure/ input/ result/ variants of each model
└── index.ts                   # barrel re-export

A helpers/ directory is emitted next to schemas/ when the Prisma schema contains Json or Decimal fields, and models/ appears under schemas/ once pureModels is enabled.

Every operation file exports the schema twice. <Model><Operation>Schema is annotated as z.ZodType<Prisma.<Model><Operation>Args>, so parsed output can be handed straight to the Prisma Client; <Model><Operation>ZodSchema is the unannotated object, for when you need .extend(), .partial() or .omit().

import { UserFindManySchema } from './prisma/generated/schemas/findManyUser.schema';

const args = UserFindManySchema.parse(req.query);
const users = await prisma.user.findMany(args);

Model schemas

Set pureModels: true to also emit one plain schema per model, with no CRUD wrappers and no Prisma types:

// prisma/generated/schemas/models/User.schema.ts
import * as z from 'zod';

export const UserSchema = z.object({
  id: z.number().int(),
  email: z.string(),
  name: z.string().nullish(),
});

export type UserType = z.infer<typeof UserSchema>;

Validators declared in the Prisma schema

@zod annotations in triple-slash comments are compiled into the generated field schemas:

model User {
  id    Int     @id @default(autoincrement())
  /// @zod.email()
  email String  @unique
  /// @zod.min(2).max(80)
  name  String?
}

See Zod comment annotations for the supported validators, custom imports, and metadata annotations.

Already using prisma-json-types-generator?

PZG can read prisma-json-types-generator (PJTG) annotations directly, so the /// [TypeName] and /// ![<ts type>] comments you already have start validating at runtime. No annotation changes, no schema changes:

{
  "typedJson": {
    "schemaModule": "./json-types"
  }
}
model Workflow {
  id Int @id @default(autoincrement())
  /// [WorkflowNode]
  nodes Json
  /// !['draft' | 'published']
  status String
}
// objects/WorkflowCreateInput.schema.ts
  nodes: z.union([JsonNullValueInputSchema, WorkflowNodeSchema]),
  status: z.enum(['draft', 'published'])

This is additive, not a migration. Keep PJTG installed. PJTG types the generated Prisma Client so prisma.workflow.findFirst() returns a typed nodes; PZG cannot do that and does not try. What PZG adds is the half PJTG leaves out: runtime validation, including on String/Int/Float literal unions that neither library checked before. Add "emitNamespace": true and the Zod schema becomes the single authored definition, with the PrismaJson type derived from it via z.infer, so the two cannot drift.

Fields with no annotation are untouched, and with no typedJson block output is unchanged. See prisma-json-types-generator interop for the option reference, the supported ![...] conversions, and the limitations.

Configuration

Options can be set in the generator block or in a JSON file next to schema.prisma. zod-generator.config.json is discovered automatically; any other path can be passed via config = "./my-config.json" (resolved relative to the schema file).

{
  "$schema": "../node_modules/prisma-zod-generator/lib/config/schema.json",
  "mode": "full",
  "pureModels": true,
  "useMultipleFiles": true
}

The $schema line gives autocomplete, hover docs, and validation in any JSON-aware editor; adjust the relative path to match your layout. Commonly used options: mode (full | minimal | custom), output, useMultipleFiles / singleFileName, pureModels, variants, naming, optionalFieldBehavior, dateTimeStrategy, strictMode, emit, zodImportTarget, zodImportPath. useMultipleFiles: false collapses the output into a single schemas.ts.

The recipes/ directory holds ready-made config presets (minimal CRUD, models only, single file, tRPC-optimized, and more); each one is a zod-generator.config.json with a short README explaining the trade-offs.

PZG Pro

The generator above is MIT licensed and complete on its own. The commercial packs generate the layers people usually hand-write around it:

  • Server Actions: typed Next.js 'use server' actions per model, validation already wired in.
  • Form UX: working forms from your schema for shadcn, MUI, Chakra, Mantine, or plain elements, with react-hook-form and your Zod schemas connected.
  • Policies & Redaction: turn /// @policy and /// @pii comments in your schema into enforced access rules and redacted logs, plus PostgreSQL row-level security.
  • Drift Guard: a CI command that compares two git refs and fails the build on a breaking schema change, with an allow-list for the ones you accept.
  • SDK Publisher: a publishable TypeScript or Python client for your models.
  • API Docs, Contract Testing, Data Factories, Performance, Multi-Tenant Kit: OpenAPI plus a mock server, Pact consumer and provider tests, seed factories typed to your schema, precompiled generation for large schemas, and tenant isolation enforced in the client.
Pack Minimum plan
Server Actions, Form UX Starter
Policies & Redaction, Drift Guard, PostgreSQL RLS, SDK Publisher, Performance Professional
API Docs, Contract Testing, Data Factories Business
Multi-Tenant Kit Enterprise

Pro code ships inside the published package, is unlocked with a PZG_LICENSE_KEY, and is invoked through the bundled pzg-pro CLI. Pro features · Pricing

Buying a licence

Licences are sold through GitHub Sponsors, which needs one thing spelled out: open the One-time tab, the second one, and pick a yearly PZG Starter, PZG Professional, PZG Business, or PZG Enterprise tier. The monthly support tiers on the first tab do not include a PZG Pro licence.

GitHub Sponsors One-time tab showing the PZG yearly tiers

https://github.com/sponsors/omar-dulaimi

Sponsor

Sponsorships fund maintenance and new feature work, separately from any Pro licence: https://github.com/sponsors/omar-dulaimi

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md for the repository layout, commit conventions, and test commands. Please open an issue before large refactors, and keep diffs focused.

License

MIT © Omar Dulaimi. See LICENSE. PZG Pro packs are covered by a separate commercial license (LICENSE-PRO.md).

About

Prisma 2+ generator to emit Zod schemas from your Prisma schema

Resources

Contributing

Security policy

Stars

824 stars

Watchers

5 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages