UNPKG

prisma-zod-generator

Version:

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

228 lines 9.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StrictModeResolver = void 0; exports.createStrictModeResolver = createStrictModeResolver; /** * Default strict mode configuration */ const DEFAULT_STRICT_MODE = { enabled: true, operations: true, objects: true, variants: true, }; /** * Utility class for resolving strict mode settings based on configuration hierarchy */ class StrictModeResolver { constructor(config) { this.config = config; } /** * Normalize operation names for backward compatibility * Maps both short names (create, update) and full names (createOne, updateOne) */ normalizeOperationName(operationName) { const operationMapping = { findMany: ['findMany'], findUnique: ['findUnique'], findFirst: ['findFirst'], findUniqueOrThrow: ['findUniqueOrThrow'], findFirstOrThrow: ['findFirstOrThrow'], create: ['create', 'createOne'], createOne: ['create', 'createOne'], createMany: ['create', 'createMany'], createManyAndReturn: ['createManyAndReturn'], update: ['update', 'updateOne'], updateOne: ['update', 'updateOne'], updateMany: ['update', 'updateMany'], updateManyAndReturn: ['updateManyAndReturn'], delete: ['delete', 'deleteOne'], deleteOne: ['delete', 'deleteOne'], deleteMany: ['delete', 'deleteMany'], upsert: ['upsert', 'upsertOne'], upsertOne: ['upsert', 'upsertOne'], aggregate: ['aggregate'], groupBy: ['groupBy'], count: ['count'], }; return operationMapping[operationName] || [operationName]; } /** * Resolve whether strict mode should be applied for a given context */ shouldApplyStrictMode(context) { const globalConfig = this.config.strictMode || {}; const schemaDefault = this.getGlobalSchemaTypeSetting(context.schemaType, globalConfig); return this.resolveFromHierarchy(context, schemaDefault); } /** * Resolve strict mode from the complete hierarchy */ resolveFromHierarchy(context, defaultValue) { var _a, _b; const { modelName, variant } = context; let result = defaultValue; // Apply model-level overrides if applicable if (modelName) { const modelConfig = (_b = (_a = this.config.models) === null || _a === void 0 ? void 0 : _a[modelName]) === null || _b === void 0 ? void 0 : _b.strictMode; if (modelConfig) { result = this.resolveModelSpecific(context, modelConfig, result); } } // Apply variant-level overrides if applicable if (variant && modelName) { result = this.resolveVariantSpecific(modelName, variant, result); } return result; } /** * Get global setting for a specific schema type */ getGlobalSchemaTypeSetting(schemaType, globalConfig) { var _a; switch (schemaType) { case 'operation': if (globalConfig.operations !== undefined) return globalConfig.operations; if (globalConfig.enabled !== undefined) return globalConfig.enabled; return DEFAULT_STRICT_MODE.operations; case 'object': if (globalConfig.objects !== undefined) return globalConfig.objects; if (globalConfig.enabled !== undefined) return globalConfig.enabled; return DEFAULT_STRICT_MODE.objects; case 'variant': if (globalConfig.variants !== undefined) return globalConfig.variants; if (globalConfig.enabled !== undefined) return globalConfig.enabled; return DEFAULT_STRICT_MODE.variants; default: return (_a = globalConfig.enabled) !== null && _a !== void 0 ? _a : DEFAULT_STRICT_MODE.enabled; } } /** * Resolve model-specific strict mode settings */ resolveModelSpecific(context, modelConfig, defaultValue) { var _a, _b, _c; const { operation, schemaType, variant } = context; // Start with the provided default, but allow model-level enabled to reset the baseline let result = defaultValue; if (modelConfig.enabled !== null && modelConfig.enabled !== undefined) { result = modelConfig.enabled; } // Check operation-specific settings if (operation && schemaType === 'operation') { const operationVariants = this.normalizeOperationName(operation); // Check if any variant of the operation is in exclude list if ((_a = modelConfig.exclude) === null || _a === void 0 ? void 0 : _a.some((excludedOp) => operationVariants.some((variant) => this.normalizeOperationName(excludedOp).includes(variant)))) { return false; } // Check operations setting if (modelConfig.operations !== null && modelConfig.operations !== undefined) { if (typeof modelConfig.operations === 'boolean') { return modelConfig.operations; } if (Array.isArray(modelConfig.operations)) { return modelConfig.operations.some((allowedOp) => operationVariants.some((variant) => this.normalizeOperationName(allowedOp).includes(variant))); } } } // Check object-specific settings if (schemaType === 'object' && modelConfig.objects !== null && modelConfig.objects !== undefined) { return modelConfig.objects; } // Check variant-specific settings in model config if (variant && ((_b = modelConfig.variants) === null || _b === void 0 ? void 0 : _b[variant]) !== null && ((_c = modelConfig.variants) === null || _c === void 0 ? void 0 : _c[variant]) !== undefined) { const variantValue = modelConfig.variants[variant]; return variantValue; } return result; } /** * Resolve variant-specific strict mode settings */ resolveVariantSpecific(modelName, variant, defaultValue) { var _a, _b, _c, _d; const variantConfig = (_a = this.config.variants) === null || _a === void 0 ? void 0 : _a[variant]; const modelVariantConfig = (_d = (_c = (_b = this.config.models) === null || _b === void 0 ? void 0 : _b[modelName]) === null || _c === void 0 ? void 0 : _c.variants) === null || _d === void 0 ? void 0 : _d[variant]; // Check model-specific variant config first (highest priority) if ((modelVariantConfig === null || modelVariantConfig === void 0 ? void 0 : modelVariantConfig.strictMode) !== null && (modelVariantConfig === null || modelVariantConfig === void 0 ? void 0 : modelVariantConfig.strictMode) !== undefined) { return modelVariantConfig.strictMode; } // Check global variant config if ((variantConfig === null || variantConfig === void 0 ? void 0 : variantConfig.strictMode) !== null && (variantConfig === null || variantConfig === void 0 ? void 0 : variantConfig.strictMode) !== undefined) { return variantConfig.strictMode; } return defaultValue; } /** * Convenience method for operation schemas */ shouldApplyStrictModeToOperation(modelName, operation) { return this.shouldApplyStrictMode({ modelName, operation, schemaType: 'operation', }); } /** * Convenience method for object schemas */ shouldApplyStrictModeToObject(modelName) { return this.shouldApplyStrictMode({ modelName, schemaType: 'object', }); } /** * Convenience method for variant schemas */ shouldApplyStrictModeToVariant(modelName, variant) { return this.shouldApplyStrictMode({ modelName, variant, schemaType: 'variant', }); } /** * Get the strict mode suffix to append to schema definitions */ getStrictModeSuffix(context) { return this.shouldApplyStrictMode(context) ? '.strict()' : ''; } /** * Get the strict mode suffix for operation schemas */ getOperationStrictModeSuffix(modelName, operation) { return this.shouldApplyStrictModeToOperation(modelName, operation) ? '.strict()' : ''; } /** * Get the strict mode suffix for object schemas */ getObjectStrictModeSuffix(modelName) { return this.shouldApplyStrictModeToObject(modelName) ? '.strict()' : ''; } /** * Get the strict mode suffix for variant schemas */ getVariantStrictModeSuffix(modelName, variant) { return this.shouldApplyStrictModeToVariant(modelName, variant) ? '.strict()' : ''; } } exports.StrictModeResolver = StrictModeResolver; /** * Create a strict mode resolver instance */ function createStrictModeResolver(config) { return new StrictModeResolver(config); } //# sourceMappingURL=strict-mode-resolver.js.map