@webilix/nestjs-helper
Version:
Helper library for NestJS
52 lines • 2.91 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.ArrayPipe = 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 ArrayPipe = class ArrayPipe {
constructor(title, options) {
this.title = title;
this.options = options;
}
transform(value) {
var _a, _b, _c, _d, _e, _f;
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.optional) && helper_library_1.Helper.IS.empty(value))
return null;
const values = typeof value === 'string' ? value.split(((_b = this.options) === null || _b === void 0 ? void 0 : _b.split) || '||') : value;
if (helper_library_1.Helper.IS.empty(values))
errors_1.Errors.throw(errors_1.Errors.undefined(this.title));
const format = (_c = this.options) === null || _c === void 0 ? void 0 : _c.format;
if (format)
values.forEach((value) => {
if (!formats_1.FormatsEnum[format].validate(value))
errors_1.Errors.throw(errors_1.Errors.invalid(this.title));
});
const minCount = (_d = this.options) === null || _d === void 0 ? void 0 : _d.minCount;
if (minCount && values.length < minCount)
errors_1.Errors.throw(errors_1.Errors.minCount(this.title, minCount));
const maxCount = (_e = this.options) === null || _e === void 0 ? void 0 : _e.maxCount;
if (maxCount && values.length > maxCount)
errors_1.Errors.throw(errors_1.Errors.maxCount(this.title, maxCount));
const unique = (_f = this.options) === null || _f === void 0 ? void 0 : _f.unique;
if (unique && !helper_library_1.Helper.IS.ARRAY.unique(values))
errors_1.Errors.throw(errors_1.Errors.unique(this.title));
return values;
}
};
exports.ArrayPipe = ArrayPipe;
exports.ArrayPipe = ArrayPipe = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [String, Object])
], ArrayPipe);
//# sourceMappingURL=array.pipe.js.map