prisma-autozod

I got tired of having to manually create Zod schemas for my Prisma models and of updating them everytime I made schema changes. This provides a way of automatically generating them with your prisma migrations.

About The Project

I got tired of having to manually create Zod schemas for my Prisma models and of updating them everytime I made schema changes. This provides a way of automatically generating them with your prisma

Built With

Getting Started

To get a local copy up and running follow these simple steps.

Installation

  1. Ensure your tsconfig.json enables the compiler's strict mode. Zod requires it and so do we, you will experience TS errors without strict mode enabled

  2. Add prisma-autozod as a dev dependency

    yarn add prisma-autozod
  3. Add the prisma-autozod generator to your schema.prisma

    generator zod {
      provider                 = "prisma-autozod"
      output                   = "./zod" // (default) the directory where generated zod schemas will be saved
     
      relationModel            = true // (default) Create and export both plain and related models.
      // relationModel         = "default" // Do not export model without relations.
      // relationModel         = false // Do not generate related model
     
      modelCase                = "PascalCase" // (default) Output models using pascal case (ex. UserModel, PostModel)
      // modelCase             = "camelCase" // Output models using camel case (ex. userModel, postModel)
     
      modelSuffix              = "Model" // (default) Suffix to apply to your prisma models when naming Zod schemas
     
      // useDecimalJs          = false // (default) represent the prisma Decimal type using as a JS number
      useDecimalJs             = true // represent the prisma Decimal type using Decimal.js (as Prisma does)
     
      imports                  = null // (default) will import the referenced file in generated schemas to be used via imports.someExportedVariable
     
      // https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values
      prismaJsonNullability    = true // (default) uses prisma's scheme for JSON field nullability
      // prismaJsonNullability = false // allows null assignment to optional JSON fields
    }
  4. Run npx prisma generate or yarn prisma generate to generate your zod schemas

  5. Import the generated schemas form your selected output location