UNPKG

prisma-zod-generator

Version:

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

112 lines (111 loc) 4.11 kB
/** * Zod Integration Helper * * Integrates @zod comment parsing with existing comment processing * and transformer workflows while maintaining backward compatibility. */ import { DMMF } from '@prisma/generator-helper'; import { CustomImport } from '../parsers/zod-comments'; /** * Interface for field with enhanced Zod validation information */ export interface EnhancedFieldInfo { field: DMMF.Field; hasZodAnnotations: boolean; zodSchema?: string; zodImports: Set<string>; zodErrors: string[]; fallbackToDefault: boolean; customImports: CustomImport[]; customSchema?: string; } /** * Interface for model with enhanced field information */ export interface EnhancedModelInfo { model: DMMF.Model; enhancedFields: EnhancedFieldInfo[]; allZodImports: Set<string>; hasAnyZodAnnotations: boolean; zodProcessingErrors: string[]; modelCustomImports: CustomImport[]; allCustomImports: CustomImport[]; } /** * Process models to extract and integrate @zod annotations * * This function processes all models and their fields to: * 1. Extract @zod annotations from field comments * 2. Generate Zod schema strings for annotated fields * 3. Maintain compatibility with existing comment processing * 4. Collect import requirements and error information * * @param models - Array of Prisma DMMF models * @param options - Processing options * @returns Enhanced model information with Zod integration */ export declare function processModelsWithZodIntegration(models: DMMF.Model[], options?: ZodIntegrationOptions): EnhancedModelInfo[]; /** * Options for Zod integration processing */ export interface ZodIntegrationOptions { /** Whether to enable @zod annotation processing (default: true) */ enableZodAnnotations?: boolean; /** Whether to generate fallback schemas for fields without annotations (default: true) */ generateFallbackSchemas?: boolean; /** Whether to validate annotation compatibility with field types (default: true) */ validateTypeCompatibility?: boolean; /** Whether to collect detailed error information (default: true) */ collectDetailedErrors?: boolean; /** Custom base types for specific field types */ customBaseTypes?: Record<string, string>; /** Zod version target for version-specific handling */ zodVersion?: 'auto' | 'v3' | 'v4'; } /** * Get integration statistics for reporting * * @param enhancedModels - Array of enhanced model information * @returns Integration statistics */ export declare function getZodIntegrationStatistics(enhancedModels: EnhancedModelInfo[]): { totalModels: number; modelsWithZodAnnotations: number; totalFields: number; fieldsWithZodAnnotations: number; totalErrors: number; uniqueImports: number; }; /** * Check if existing comment processing should be preserved * * This function ensures backward compatibility by checking if a field * comment contains existing comment directives that should be processed * by the original comment processing system. * * @param comment - Field comment string * @returns True if existing processing should be preserved */ export declare function shouldPreserveExistingCommentProcessing(comment: string): boolean; /** * Extract non-Zod comment content for backward compatibility * * @param comment - Original comment string * @returns Comment content with @zod annotations removed */ export declare function extractNonZodCommentContent(comment: string): string; /** * Integration wrapper that maintains backward compatibility * * This function provides a drop-in replacement for existing comment processing * while adding @zod annotation support. * * @param models - Array of Prisma DMMF models * @param options - Integration options * @returns Models with both existing and Zod comment processing applied */ export declare function integrateZodWithExistingComments(models: DMMF.Model[], options?: ZodIntegrationOptions): { enhancedModels: EnhancedModelInfo[]; backwardCompatibilityPreserved: boolean; integrationWarnings: string[]; };