UNPKG

bb-inspired

Version:

Core library for BB-inspired NestJS backend

83 lines 3.78 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var GlobalValidationPipe_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlobalValidationPipe = void 0; const common_1 = require("@nestjs/common"); const class_validator_1 = require("class-validator"); const class_transformer_1 = require("class-transformer"); const logger_1 = require("../utils/logger"); let GlobalValidationPipe = GlobalValidationPipe_1 = class GlobalValidationPipe { constructor(options = {}) { this.options = options; this.logger = new logger_1.AppLogger(GlobalValidationPipe_1.name); this.options = { whitelist: true, forbidNonWhitelisted: true, ...options, }; } async transform(value, metadata) { var _a; const { metatype } = metadata; if (!metatype || !this.shouldValidate(metatype)) { return value; } const object = (0, class_transformer_1.plainToClass)(metatype, value, { enableImplicitConversion: (_a = this.options.transformOptions) === null || _a === void 0 ? void 0 : _a.enableImplicitConversion, }); const errors = await (0, class_validator_1.validate)(object, { whitelist: this.options.whitelist, forbidNonWhitelisted: this.options.forbidNonWhitelisted, forbidUnknownValues: this.options.forbidUnknownValues, }); if (errors.length === 0) { return object; } const formattedErrors = this.formatErrors(errors); this.logger.warn(`Validation failed for ${metatype.name}`, { metadata: formattedErrors }); if (this.options.exceptionFactory) { throw this.options.exceptionFactory(errors); } throw new common_1.BadRequestException({ statusCode: 400, message: 'Validation failed', errors: formattedErrors, }); } shouldValidate(metatype) { const types = [String, Boolean, Number, Array, Object]; return !types.some(type => metatype === type); } formatErrors(errors) { return errors.reduce((acc, error) => { if (error.constraints) { const field = error.property; const messages = Object.values(error.constraints); acc[field] = messages; } if (error.children && error.children.length > 0) { const nestedErrors = this.formatErrors(error.children); Object.keys(nestedErrors).forEach(key => { const nestedField = `${error.property}.${key}`; acc[nestedField] = nestedErrors[key]; }); } return acc; }, {}); } }; exports.GlobalValidationPipe = GlobalValidationPipe; exports.GlobalValidationPipe = GlobalValidationPipe = GlobalValidationPipe_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [Object]) ], GlobalValidationPipe); //# sourceMappingURL=validation.pipe.js.map