@kitstack/nest-powertools
Version:
A comprehensive collection of NestJS powertools, decorators, and utilities to supercharge your backend development
53 lines • 2.43 kB
JavaScript
;
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationPipe = void 0;
const common_1 = require("@nestjs/common");
const class_validator_1 = require("class-validator");
const class_transformer_1 = require("class-transformer");
let ValidationPipe = class ValidationPipe {
constructor(options = {}) {
this.options = options;
this.options = {
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
...options,
};
}
async transform(value, { metatype }) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = (0, class_transformer_1.plainToClass)(metatype, value);
const errors = await (0, class_validator_1.validate)(object, {
whitelist: this.options.whitelist,
forbidNonWhitelisted: this.options.forbidNonWhitelisted,
});
if (errors.length > 0) {
const errorMessages = errors
.map((error) => Object.values(error.constraints || {}).join(', '))
.join('; ');
throw new common_1.BadRequestException(`Validation failed: ${errorMessages}`);
}
return this.options.transform ? object : value;
}
toValidate(metatype) {
const types = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}
};
exports.ValidationPipe = ValidationPipe;
exports.ValidationPipe = ValidationPipe = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [Object])
], ValidationPipe);
//# sourceMappingURL=validation.pipe.js.map