1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseBoolPipe = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
|
6 | const optional_decorator_1 = require("../decorators/core/optional.decorator");
|
7 | const http_status_enum_1 = require("../enums/http-status.enum");
|
8 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
9 | const shared_utils_1 = require("../utils/shared.utils");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | let ParseBoolPipe = class ParseBoolPipe {
|
18 | constructor(options) {
|
19 | this.options = options;
|
20 | options = options || {};
|
21 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST } = options;
|
22 | this.exceptionFactory =
|
23 | exceptionFactory ||
|
24 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
25 | }
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | async transform(value, metadata) {
|
34 | if ((0, shared_utils_1.isNil)(value) && this.options?.optional) {
|
35 | return value;
|
36 | }
|
37 | if (this.isTrue(value)) {
|
38 | return true;
|
39 | }
|
40 | if (this.isFalse(value)) {
|
41 | return false;
|
42 | }
|
43 | throw this.exceptionFactory('Validation failed (boolean string is expected)');
|
44 | }
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | isTrue(value) {
|
51 | return value === true || value === 'true';
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | isFalse(value) {
|
59 | return value === false || value === 'false';
|
60 | }
|
61 | };
|
62 | exports.ParseBoolPipe = ParseBoolPipe;
|
63 | exports.ParseBoolPipe = ParseBoolPipe = tslib_1.__decorate([
|
64 | (0, injectable_decorator_1.Injectable)(),
|
65 | tslib_1.__param(0, (0, optional_decorator_1.Optional)()),
|
66 | tslib_1.__metadata("design:paramtypes", [Object])
|
67 | ], ParseBoolPipe);
|