UNPKG

@webilix/nestjs-helper

Version:

Helper library for NestJS

196 lines 9.46 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidatorPipe = void 0; const common_1 = require("@nestjs/common"); const helper_library_1 = require("@webilix/helper-library"); const errors_1 = require("../errors"); const formats_1 = require("../formats"); let ValidatorPipe = class ValidatorPipe { constructor() { this.errors = []; } setError(error) { this.errors.push(error); } transform(values, meta) { this.errors = []; const conditions = Reflect.getMetadata('validator', meta.metatype || {}); if (conditions) { values = this.updateValues(conditions, values); this.validate(conditions, values); } if (this.errors.length !== 0) throw new common_1.HttpException(this.errors, common_1.HttpStatus.BAD_REQUEST); return values; } updateValues(conditions, values) { const update = (condition, value) => { if (helper_library_1.Helper.IS.empty(value)) return value; switch (condition.type) { case 'BOOLEAN': case 'NUMBER': return value; case 'DATE': if (!helper_library_1.Helper.IS.STRING.jsonDate(value)) return value; value = helper_library_1.Helper.STRING.changeNumbers(value, 'EN'); if (!condition.omitConvert) value = new Date(value); return value; case 'STRING': if (!helper_library_1.Helper.IS.string(value)) return value; if (!condition.omitTrim) value = value.trim(); if (condition.format) value = helper_library_1.Helper.STRING.changeNumbers(value, 'EN'); else if (condition.changeNumbers) value = helper_library_1.Helper.STRING.changeNumbers(value, condition.changeNumbers); return value; case 'OBJECT': const properties = Object.keys(condition.properties); properties.forEach((property) => { if (helper_library_1.Helper.IS.empty(value[property])) return; value[property] = update(condition.properties[property], value[property]); }); return value; } }; Object.keys(conditions).forEach((key) => { const condition = conditions[key]; if (condition.array) { if (helper_library_1.Helper.IS.array(values[key])) values[key].map((_, index) => update(condition, values[key][index])); } else values[key] = update(condition, values[key]); }); return values; } validate(conditions, values) { Object.keys(conditions).forEach((key) => { const condition = conditions[key]; if (condition.array) this.validateArray(condition, values[key]); else this.validateValue(condition, values[key]); }); } validateType(condition, value) { if (helper_library_1.Helper.IS.empty(value)) return true; switch (condition.type) { case 'BOOLEAN': return helper_library_1.Helper.IS.boolean(value); case 'DATE': return helper_library_1.Helper.IS.date(value) || helper_library_1.Helper.IS.STRING.jsonDate(value); case 'NUMBER': return helper_library_1.Helper.IS.number(value); case 'OBJECT': return helper_library_1.Helper.IS.object(value); case 'STRING': return helper_library_1.Helper.IS.string(value); } } validateArray(condition, value) { if (!condition.array) return; const title = condition.title; // UNDEFINED if (value === undefined) return this.setError(errors_1.Errors.undefined(title)); // NULLABLE const isEmpty = helper_library_1.Helper.IS.empty(value); if (!condition.nullable && isEmpty) return this.setError(errors_1.Errors.empty(title)); if (isEmpty) return; // TYPE if (!helper_library_1.Helper.IS.array(value)) return this.setError(errors_1.Errors.invalid(title)); // COUNT && UNIQUE if (condition.array !== true) { if (condition.array.minCount && value.length < condition.array.minCount) return this.setError(errors_1.Errors.minCount(title, condition.array.minCount)); if (condition.array.maxCount && value.length > condition.array.maxCount) return this.setError(errors_1.Errors.maxCount(title, condition.array.maxCount)); if (condition.array.unique && !helper_library_1.Helper.IS.ARRAY.unique(value, condition.array.unique === true ? undefined : condition.array.unique)) return this.setError(errors_1.Errors.unique(title)); } // Change nullable option for array values condition = Object.assign(Object.assign({}, condition), { nullable: false }); value.forEach((_, index) => this.validateValue(condition, value[index], index + 1)); } validateValue(condition, value, index, parent) { const title = (parent ? `${parent}: ` : '') + condition.title + (index ? ` ${helper_library_1.Helper.NUMBER.getTitle(index)}` : ''); // UNDEFINED if (value === undefined) return this.setError(errors_1.Errors.undefined(title)); // NULLABLE const isEmpty = helper_library_1.Helper.IS.empty(value); if (!condition.nullable && isEmpty) return this.setError(errors_1.Errors.empty(title)); if (isEmpty) return; // TYPE if (!this.validateType(condition, value)) return this.setError(errors_1.Errors.invalid(title)); // CONDITIONS switch (condition.type) { case 'BOOLEAN': return; case 'DATE': return this.validateDate(condition, title, new Date(value)); case 'NUMBER': return this.validateNumber(condition, title, +value); case 'STRING': return this.validateString(condition, title, value.toString()); case 'OBJECT': const properties = Object.keys(condition.properties); properties.forEach((property) => this.validateValue(condition.properties[property], value[property], undefined, title)); return; } } validateDate(condition, title, value) { if (condition.minDate && value.getTime() < condition.minDate.getTime()) return this.setError(errors_1.Errors.minDate(title, condition.minDate)); if (condition.maxDate && value.getTime() > condition.maxDate.getTime()) return this.setError(errors_1.Errors.maxDate(title, condition.maxDate)); } validateNumber(condition, title, value) { if (condition.minimum && value < condition.minimum) return this.setError(errors_1.Errors.minimum(title, condition.minimum)); if (condition.maximum && value > condition.maximum) return this.setError(errors_1.Errors.maximum(title, condition.maximum)); if (condition.enum && !condition.enum.includes(value)) return this.setError(errors_1.Errors.invalid(title)); } validateString(condition, title, value) { if (condition.format && !formats_1.FormatsEnum[condition.format].validate(value)) return this.setError(errors_1.Errors.invalid(title)); if (condition.enum && !condition.enum.includes(value)) return this.setError(errors_1.Errors.invalid(title)); if (condition.length && value.length !== condition.length) return this.setError(errors_1.Errors.eqLength(title, condition.length)); if (condition.minLength && value.length < condition.minLength) return this.setError(errors_1.Errors.minLength(title, condition.minLength)); if (condition.maxLength && value.length > condition.maxLength) return this.setError(errors_1.Errors.maxLength(title, condition.maxLength)); if (condition.pattern && !condition.pattern.test(value)) return this.setError(errors_1.Errors.invalid(title)); } }; exports.ValidatorPipe = ValidatorPipe; exports.ValidatorPipe = ValidatorPipe = __decorate([ (0, common_1.Injectable)() ], ValidatorPipe); //# sourceMappingURL=validator.pipe.js.map