Skip to main content

Generated Zod Schemas

Prisma Guard parses your Prisma schemas and outputs structured, optimized Zod schemas and TypeScript types. Learn how schemas are generated, their directory structures, and file contents.


Generation Commands

Run the generator using the CLI:

# Generate both runtime protection mapping metadata and Zod schemas
npx prisma-guard

# Generate ONLY the Zod validation schemas
npx prisma-guard zod

Directory Structure

By default, Prisma Guard writes generated Zod validation modules into your configured outputDir (or node_modules/.prisma-guard if unspecified).

Here is the directory tree produced by the compiler:

generated/
├── index.ts # Global entrypoint exporting all model schemas and types
├── lib/
│ └── constants.ts # Persistent, user-editable constants and validation messages
└── zod/
├── user.ts # Zod schemas, enums, and types for the User model (kebab-case)
└── blog-post.ts # Zod schemas, enums, and types for the BlogPost model
  • zod/ Directory: Cleared and fully rebuilt on every generation run. Never edit files in this folder directly.
  • lib/ Directory: Persistent and never overwritten, allowing you to customize error messages or define reusable variables.

Model Schema Layout

Each file generated inside the zod/ folder represents a model (using kebab-case file naming) and contains:

  1. Base Schema: A private BaseSchema defining the common validation rules.
  2. Public Schemas: CreateSchema and UpdateSchema (which applies .partial()).
  3. Scalar Schemas: Complete database-level schemas bypassing omissions (unless skipScalar: true).
  4. TypeScript Types: Fully inferred types representing inputs and outputs.

Example Schema Output

Given a standard User model, here is the structure of the generated TypeScript file:

// generated/zod/user.ts
import { z } from "zod";
import { REQUIRED_MESSAGE } from "../lib/constants";

// 1. Private Base Object
const UserBaseSchema = z.object({
email: z.string().trim().email(),
name: z.string().trim().min(2),
});

// 2. Public Create Schema
export const UserCreateSchema = UserBaseSchema;

// 3. Public Update Schema (automatically partial)
export const UserUpdateSchema = UserBaseSchema.partial();

// 4. Inferred TypeScript Types
export type UserCreate = z.infer<typeof UserCreateSchema>;
export type UserInput = z.input<typeof UserCreateSchema>;
export type UserUpdate = z.infer<typeof UserUpdateSchema>;

Enum Translations

Prisma Guard parses all enums in your Prisma schema and generates matching Zod enum validators. The suffix configured in enumSuffix (default: Enum) is appended to the enum name:

Prisma Definition

enum UserRole {
ADMIN
USER
GUEST
}

Generated Zod Enum

export const UserRoleEnum = z.enum(["ADMIN", "USER", "GUEST"]);

export type UserRole = z.infer<typeof UserRoleEnum>;