MCPcopy Index your code
hub / github.com/chrishoermann/zod-prisma-types

github.com/chrishoermann/zod-prisma-types @v3.5.8

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.5.8 ↗ · + Follow
446 symbols 1,258 edges 174 files 36 documented · 8% 2 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

NPM version Stars Contirbutors License Issues

zod-prisma-types

zod-prisma-types is a generator for prisma that generates zod schemas from your prisma models. This includes schemas of models, enums, inputTypes, argTypes, filters and so on. It also provides options to write advanced zod validators directly in the Prisma schema comments.

Since I'm maintaining the generator in my spare time consider buying me a coffee or sponsoring me if you like the project. Thanks!

"Buy Me A Coffee"

⚠️ Maintenance Notice & Recommendation

🕐 Limited Maintenance Mode
Due to time constraints, this package will only receive critical bug fixes and essential updates. I no longer use Prisma as extensively in my projects, which means I don't personally benefit from new generator features that much anymore.

🚀 Recommendation for New Projects
For new projects, please consider using prisma-zod-generator by Omar Dulaimi. Omar maintains multiple Prisma generators and, I believe, has much deeper expertise in this domain than I do. It also seems to have quite similar features.

💡 Why the Alternative?
Omar's active involvement with multiple Prisma generators means better feature coverage, more frequent updates, and deeper integration with the Prisma ecosystem.


📦 Current Status: Maintenance mode - critical fixes only
🔄 Recommended Alternative: prisma-zod-generator
👤 Maintainer: Omar Dulaimi

Supported versions

  • Prisma: Currently this package supports Prisma versions 4.x - 6.x.
  • Zod: Currently this package supports Zod versions 3.x - 4.x.

Make shure zod is installed as a dependency and not a devDependency for the generator to pick it up correctly!

Breaking changes in v2.x.x

Be aware that some generator options have been removed, a few new ones have been added, the behavior of custom imports has changed and ts-morph is no longer needed to generate files in v2.0.0.

Breaking changes in v3.x.x

  • If you have used decimal or Json values you might encounter changed behavior in v3.x.x. Please read the decimal and json sections for more information. If not you can safely upgrade to v3.x.x.
  • Imports are now generally handled at field level except for model validators. This enables the generator to add the custom validation logic on input type level like whereUnique inputs. If you have used custom imports in v2.x.x you should change the syntax to the new one. Please read the custom imports section for more information.
  • validateWhereUniqueInputs is now true by default

Known issues and Limitations

  • Lowercase model names will result in Errors and duplicate schema names in the generated schemas. Please use uppercase model names and prismas @@map() directive when using lowercase table names in the datebase to avoid this issue.
  • The satisfies operator: As some people have pointed out the input and output schemas and some of the relation schemas are typed as z.ZodType<myType>. This is required by zod for recursice types to work properly as stated in the docs. This has the downside that some zod methods like .merge(), .omit(), etc. are not available on these types.

Known bugs

  • if you need to add rich comments to your prisma file alongside the validator string, always add the validator string last. The regex to match the strings currently has some problems validating the strings when a comment comes after the validator string

Table of contents

About this project

For one of my projects I was in need of a generator that offers the possibility of adding zod validators directly in prisma schema's rich-comments and generates zod schemas for all prisma models, enums, inputTypes, argTypes, filters and so on. I also wanted to be able to import these schemas in the frontend e.g. for form validation and make the generator as flexible as possible so it covers a large range of use cases. Since there were no generators out there that met my requirements or they weren't actively maintained at that time I decided to write zod-prisma-type.

Installation

via npm:

npm install zod-prisma-types

via yarn:

yarn add zod-prisma-types

via pnpm:

pnpm add zod-prisma-types

tsconfig.json

For the generator to work properly you have to set the following options in your tsconfg.json:

strict: true,
strictNullChecks: true,

Usage

Supports prisma 4.x - 5.x

Add the following code to your schema.prisma file:

generator zod {
  provider       = "zod-prisma-types"
}

Then run the following command to create a single index.ts file in the ./generated/zod output folder containing all the zod prisma schemas:

npx prisma generate zod

Then import the schema's into your file:

import { mySchema } from '/prisma/generated/zod'; // All schemas are here by default, use the 'output' option to change it

If you encounter errors like the following /bin/sh: zod-prisma-types: command not found please try to use the npx command with the zod-prisma-types command.

generator zod {
  provider       = "npx zod-prisma-types"
}

If you want to customize the behavior of the generator you can use the following options:

generator zod {
  provider                         = "ts-node-dev ../generator/src/bin.ts"
  output                           = "./generated/zod" // default is ./generated/zod
  useMultipleFiles                 = true // default is false
  writeBarrelFiles                 = false // default is true
  createInputTypes                 = false // default is true
  createModelTypes                 = false // default is true
  addInputTypeValidation           = false // default is true
  addIncludeType                   = false // default is true
  addSelectType                    = false // default is true
  validateWhereUniqueInput         = false // default is true
  createOptionalDefaultValuesTypes = true // default is false
  createRelationValuesTypes        = true // default is false
  createPartialTypes               = true // default is false
  useDefaultValidators             = false // default is true
  coerceDate                       = false // default is true
  writeNullishInModelTypes         = true // default is false
  prismaClientPath                 = "./path/to/prisma/client" // default is client output path
}

useMultipleFiles

default: false

If you want to create multiple files instead of a single index.ts file you can set this option to true. This will create a file for each model, enum, inputType, argType, filter, etc. The files will be created in sub folders in the specified output folder and a barrel file will be added at the root of the output folder.

generator zod {
  // ...rest of config
  useMultipleFiles = false
}

output

default: ./generated/zod

Provide an alternative output path.

writeBarrelFiles

default: true

If you use useMultipleFiles and do not want to create a barrel file for each sub folder you can set this option to false. This will create an index.ts file in each sub folder that exports all the files in the folder. This option may be beneficial for typescript performance on big schemas.

generator zod {
  // ...rest of config
  writeBarrelFiles = false
}

createInputTypes

default: true

If you just want to create zod schemas for your models and enums you can disable the creation of the corresponding input types. This may be useful if you just want to use zod schemas of your models for validating input types in react-hook-form or some similar use cases.

generator zod {
  // ...rest of config
  createInputTypes = false
}

createModelTypes

default: true

If you just want to create zod schemas for your input types you can disable the creation of the corresponding model schemas. This may be useful if you just want to use the zod input schemas for autocompletion in your trpc queries or similar use cases.

generator zod {
  // ...rest of config
  createModelTypes = false
}

addInputTypeValidation

default: true

If you want to use your custom zod validators that you added via rich-comments only on your generated model schemas but not on your created input type schemas (UserCreateInput, UserUpdateManyInput, etc.) you can disable this feature.

generator zod {
  // ...rest of config
  addInputTypeValidation = false
}

addIncludeType

default: true

By default the include type is added to the [Model]ArgTypeSchema. If you don't want to add a zod schema for the include type you can set this option to false.

generator zod {
  // ...rest of config
  addIncludeType = false
}

addSelectType

default: true

By default the select type is added to the [Model]ArgTypeSchema. If you don't want to add a zod schema for the select type you can set this option to false.

generator zod {
  // ...rest of config
  addSelectType = false
}

validateWhereUniqueInput

default: false

By default the generator will not validate the whereUnique input types in multifile mode since a bunch of unused imports will often be generated. If you want to validate the whereUnique input types you can set this option to true.

Be aware that this can lead to eslint errors if you use the no-unused-vars rule which you need to resolve manually.

generator zod {
  // ...rest of config
  validateWhereUniqueInput = true
}

createOptionalDefaultValuesTypes

default: false

If you want to have a schema of your model where fields with default values are marked as .optional() you can pass the following config option:

```prisma generator zod { // ...rest of config createOptionalDefaultValuesTypes = true }

model ModelWithDefaultValues { id Int @id @default(autoincrement()) string String @default("default") otherString String int Int @default(1) otherInt Int float Float @default(1.1) otherFlo

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 230
Function 119
Class 80
Interface 17

Languages

TypeScript100%

Modules by API surface

packages/generator/src/classes/extendedDMMFSchemaField.ts35 symbols
packages/generator/src/classes/extendedDMMFInputType.ts20 symbols
packages/generator/src/classes/extendedDMMF.ts16 symbols
packages/generator/src/classes/fileWriter.ts15 symbols
packages/generator/src/classes/extendedDMMFSchemaArg.ts14 symbols
packages/generator/src/classes/extendedDMMFModel/02_extendedDMMFModelFlags.ts14 symbols
packages/generator/src/classes/extendedDMMFOutputType.ts13 symbols
packages/generator/src/classes/extendedDMMFSchema.ts11 symbols
packages/generator/src/classes/extendedDMMFField/01_extendedDMMFFieldBase.ts11 symbols
packages/generator/src/classes/extendedDMMFModel/01_extendedDMMFModelBase.ts10 symbols
packages/generator/src/classes/extendedDMMFField/05_extendedDMMFFieldDefaultValidators.ts10 symbols
packages/generator/src/classes/extendedDMMFField/11_extendedDMMFFieldOmitField.ts9 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add zod-prisma-types \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page