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!
🕐 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
Make shure
zodis installed as adependencyand not adevDependencyfor the generator to pick it up correctly!
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.
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
- 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
satisfiesoperator: As some people have pointed out theinputandoutputschemas and some of therelationschemas are typed asz.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.
tsconfig.jsonuseMultipleFilesoutputwriteBarrelFilescreateInputTypescreateModelTypesaddInputTypeValidationaddIncludeTypeaddSelectTypevalidateWhereUniqueInputcreateOptionalDefaultValuesTypescreateRelationValuesTypescreatePartialTypesuseDefaultValidatorscoerceDatewriteNullishInModelTypesuseTypeAssertionsprismaClientPathWrong zod typeWrong validatorTypo Errorszod-prismaFor 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.
via npm:
npm install zod-prisma-types
via yarn:
yarn add zod-prisma-types
via pnpm:
pnpm add zod-prisma-types
tsconfig.jsonFor the generator to work properly you have to set the following options in your tsconfg.json:
strict: true,
strictNullChecks: true,
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 foundplease try to use thenpxcommand with thezod-prisma-typescommand.
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
}
useMultipleFilesdefault:
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
}
outputdefault:
./generated/zod
Provide an alternative output path.
writeBarrelFilesdefault:
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
}
createInputTypesdefault:
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
}
createModelTypesdefault:
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
}
addInputTypeValidationdefault:
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
}
addIncludeTypedefault:
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
}
addSelectTypedefault:
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
}
validateWhereUniqueInputdefault:
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-varsrule which you need to resolve manually.
generator zod {
// ...rest of config
validateWhereUniqueInput = true
}
createOptionalDefaultValuesTypesdefault:
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
$ claude mcp add zod-prisma-types \
-- python -m otcore.mcp_server <graph>