Skip to main content

@core/zod

The @core/zod plugin generates Zod schemas for models and input arguments of Prisma CRUD operations. The plugin is automatically enabled when any of the following conditions meets:

info

You need to explicitly enable @core/zod plugin if you use the Express.js or Fastify server adapter with zodSchemas option enabled.

By default, the Zod schemas are generated into node_modules/.zenstack/zod directory, and are reexported through @zenstackhq/runtime/zod. If you configure the plugin to output to a custom location, you can just directly import from there.

The generated schemas have the following three parts:

  • zod/models

    The schema for validating the models, containing field's typing and validation rules. Relation fields and foreign key fields are ignored. For each model, three schemas are generated respectively:

    • [Model]Schema

      The schema for validating the model itself. All non-optional fields are required.

    • [Model]CreateSchema

      The schema for validating the data for creating the model. Similar to "[Model]Schema" but all fields with default values are marked optional.

    • [Model]UpdateSchema

      The schema for validating the data for updating the model. Similar to "[Model]Schema" but all fields are marked optional.

  • zod/input

    The schema for validating the input arguments of Prisma CRUD operations. You usually won't use them directly. The tRPC plugin relies on them to validate the input arguments in the generated routers.

  • zod/objects

    The schema for objects and enums used by the zod/input schemas. You usually won't use them directly.

Installation

This plugin is built-in to ZenStack and does not need to be installed separately.

Options

NameTypeDescriptionRequiredDefault
outputStringOutput directory (relative to the path of ZModel)Nonode_modules/.zenstack/zod
modelOnlyBooleanOnly generate schemas for the models, but not for the Prisma CRUD input argumentsNofalse
generateModelsString, String[]Array or comma separated string for the models to generate routers for.NoAll models
compileBooleanIf the generated TS code should be compiled to JSNotrue
preserveTsFilesBooleanIf the generated TS files should be preserved (after compiled to JS)Notrue if compile is set to false, otherwise false
noUncheckedInputBooleanDisables schema generation for Prisma's "Unchecked" input typesNofalse
info

When the generateModels option is used to specify a list of models to generate, the plugin will also recursively traverse and include all models that are referenced by the specified models. This can result in more code being generated than you expect.

Example

Declare the plugin in your ZModel file:

/schema.zmodel
plugin zod {
provider = '@core/zod'
}

Then you can import and use the generated schemas:

import { PostCreateSchema } from '@zenstackhq/runtime/zod/models';

PostCreateSchema.parse(data);

You can turn off the compile option and use a custom output location if you want the generated Zod schema to be compiled along with your own Typescript project:

/schema.zmodel
plugin zod {
provider = '@core/zod'
output = 'src/lib/zod'
compile = false
}