UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

37 lines (36 loc) 1.76 kB
/** * Shared DMMF → Zod expression mapping for the Pro packs. * * Two packs emit Zod schemas from the same DMMF (server actions and policies), * and they drifted: one referenced Prisma enums without importing them, mapped * nullable columns to `.optional()` so a real row with a null failed to parse, * and mapped `Decimal` to `z.number()` so the value Prisma returns was rejected. * Keeping one mapping means fixing those once. */ import { DMMF } from '@prisma/generator-helper'; /** * Zod expression for a field's type, without optionality. * * Enum members are inlined as string literals rather than referencing the Prisma * enum, so the emitted module needs no import beyond zod and cannot fail with * "X is not defined". */ export declare function zodBaseForField(field: DMMF.Field, enums?: readonly DMMF.DatamodelEnum[]): string; /** * Zod expression including nullability. * * A nullable column is `.nullable()`, not `.optional()`: Prisma returns `null` * for an unset value, and `.optional()` alone rejects it. */ export declare function zodExpressionForField(field: DMMF.Field, enums?: readonly DMMF.DatamodelEnum[]): string; /** Columns the database supplies, which a caller must not send. */ export declare function isDatabaseGeneratedField(field: DMMF.Field): boolean; /** Optional on create when the schema itself can supply a value. */ export declare function isOptionalOnCreate(field: DMMF.Field): boolean; /** * Of `candidates`, the field names the model actually has. * * Zod 4 throws "Unrecognized key" when a mask names a key the object lacks, so * an omit list has to be filtered against the model rather than hardcoded. */ export declare function presentFieldNames(model: DMMF.Model, candidates: string[]): string[];