npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cvsa/prisma-zod

v3.6.0

Published

A [Prisma generator](https://www.prisma.io/docs/orm/prisma-schema/overview/generators) that automatically generates [Zod](https://zod.dev) schemas from your Prisma models.

Readme

prisma-zod

A Prisma generator that automatically generates Zod schemas from your Prisma models.

Prerequisites

Installation

[!NOTE] This package is ESM only.

Install it with your preferred package manager:

npm i -D @cvsa/prisma-zod
yarn add -D @cvsa/prisma-zod
pnpm i -D @cvsa/prisma-zod
bun i -D @cvsa/prisma-zod
deno add -D npm:@cvsa/prisma-zod

Usage

Assume your project structure looks like this:

awesome-project/
├── prisma/
│   ├── migrations/
│   └── schema.prisma
├── prisma.config.ts
└── package.json

Add the generator to your schema.prisma:

generator zod {
    provider = "prisma-zod"
    output   = "./zod"
}

Then run prisma generate and import the schemas:

import { LogSchema } from "root/prisma/zod";

Using with prisma-json-types-generator

If you have prisma-json-types-generator installed and have already defined types for your Json fields, you can enhance runtime validation and get more precise Zod schemas by providing your own Zod schemas.

To do this, use the extendSchema option in the generator config to specify where your schemas are defined.

schema.prisma:

// ...
generator json {
    provider = "prisma-json-types-generator"
}

generator zod {
    provider     = "prisma-zod"
    output       = "./zod"
    // This file must be placed next to `schema.prisma`
    extendSchema = "zod-schema.ts"
}

model Log {
    id   Int  @id
    /// [LogMetaType]
    meta Json
}

Create zod-schema.ts next to your schema.prisma:

import z from "zod";

// Use the same name as specified in the AST comment.
// Since the generated Zod schemas will import this schema directly,
// you must export it explicitly.
export const LogMetaType = z.object({
	timestamp: z.number(),
	host: z.string(),
});

In types.ts:

import type z from "zod";
import type { LogMetaType } from "./zod-schema";

declare global {
	namespace PrismaJson {
		type LogMetaType = z.infer<typeof LogMetaType>;
	}
}

Now Log.meta will be strongly typed as { timestamp: number; host: string }, and your generated schema will include the precise Zod definition:

// typeof LogSchema
z.ZodObject<{
    id: z.ZodNumber;
    meta: z.ZodObject<{
        timestamp: z.ZodNumber;
        host: z.ZodString;
    }, z.core.$strip>;
}, z.core.$strip>

Control Output for Special DataType

By default, Prisma client generator will produce Date type for DateTime field, and bigint type for BigInt field.

However, these fields can't be serialized to JSON, so many users have introduced various workarounds to solve this (see prisma/prisma issue #5522).

If you have converted these fields to other types, you'll found a mismatch between your converted Prisma type and the Zod schemas generated by this library.

To avoid this, you can use dateSchema and bigintSchema options.

dateSchema

Controls the Zod schema generated for DateTime fields.

generator zod {
    provider  = "prisma-zod"
    output    = "./zod"
    dateSchema = "string" // "default" | "string" | "number"
}

| Option | Zod Schema | When to Use | |--------|------------|-------------| | "default" | z.coerce.date() | Standard choice, coerces input to Date | | "string" | z.string() | When your API handles dates as ISO strings | | "number" | z.number() | When you convert dates to Unix timestamps |

bigintSchema

Controls the Zod schema generated for BigInt fields.

generator zod {
    provider    = "prisma-zod"
    output      = "./zod"
    bigintSchema = "string" // "default" | "number" | "union" | "string"
}

| Option | Zod Schema | When to Use | |--------|------------|-------------| | "default" | z.bigint() | Standard choice | | "string" | z.string() | When serializing BigInt to JSON | | "number" | z.number() | When BigInt values fit in safe integer range | | "union" | z.union([z.bigint(), z.number()]) | When you need both |

Per-Field Override via AST Comments

You can override the global dateSchema / bigintSchema setting for individual fields using AST comments in your Prisma schema:

model Event {
    id          Int      @id
    /// [string]
    createdAt   DateTime // Uses z.string() regardless of dateSchema
    /// [number]
    timestamp   BigInt   // Uses z.number() regardless of bigintSchema
}

Supported overrides:

  • DateTime: /// [default] /// [string] /// [number]
  • BigInt: /// [default] /// [string] /// [number] /// [union]

Contributing

See CONTRIBUTING for development setup and guidelines.

Found a type mismatch between prisma-client and the generated Zod schema? Please open an issue.

License

MIT