1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseEnumPipe = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const core_1 = require("../decorators/core");
|
6 | const index_1 = require("../index");
|
7 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
8 | const shared_utils_1 = require("../utils/shared.utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let ParseEnumPipe = class ParseEnumPipe {
|
17 | constructor(enumType, options) {
|
18 | this.enumType = enumType;
|
19 | this.options = options;
|
20 | if (!enumType) {
|
21 | throw new Error(`"ParseEnumPipe" requires "enumType" argument specified (to validate input values).`);
|
22 | }
|
23 | options = options || {};
|
24 | const { exceptionFactory, errorHttpStatusCode = index_1.HttpStatus.BAD_REQUEST } = options;
|
25 | this.exceptionFactory =
|
26 | exceptionFactory ||
|
27 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | async transform(value, metadata) {
|
37 | if ((0, shared_utils_1.isNil)(value) && this.options?.optional) {
|
38 | return value;
|
39 | }
|
40 | if (!this.isEnum(value)) {
|
41 | throw this.exceptionFactory('Validation failed (enum string is expected)');
|
42 | }
|
43 | return value;
|
44 | }
|
45 | isEnum(value) {
|
46 | const enumValues = Object.keys(this.enumType).map(item => this.enumType[item]);
|
47 | return enumValues.includes(value);
|
48 | }
|
49 | };
|
50 | exports.ParseEnumPipe = ParseEnumPipe;
|
51 | exports.ParseEnumPipe = ParseEnumPipe = tslib_1.__decorate([
|
52 | (0, core_1.Injectable)(),
|
53 | tslib_1.__param(1, (0, core_1.Optional)()),
|
54 | tslib_1.__metadata("design:paramtypes", [Object, Object])
|
55 | ], ParseEnumPipe);
|