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 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let ParseBoolPipe = class ParseBoolPipe {
|
17 | constructor(options) {
|
18 | options = options || {};
|
19 | const { exceptionFactory, errorHttpStatusCode = http_status_enum_1.HttpStatus.BAD_REQUEST } = options;
|
20 | this.exceptionFactory =
|
21 | exceptionFactory ||
|
22 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
23 | }
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | async transform(value, metadata) {
|
32 | if (this.isTrue(value)) {
|
33 | return true;
|
34 | }
|
35 | if (this.isFalse(value)) {
|
36 | return false;
|
37 | }
|
38 | throw this.exceptionFactory('Validation failed (boolean string is expected)');
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | isTrue(value) {
|
46 | return value === true || value === 'true';
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 | isFalse(value) {
|
54 | return value === false || value === 'false';
|
55 | }
|
56 | };
|
57 | ParseBoolPipe = tslib_1.__decorate([
|
58 | (0, injectable_decorator_1.Injectable)(),
|
59 | tslib_1.__param(0, (0, optional_decorator_1.Optional)()),
|
60 | tslib_1.__metadata("design:paramtypes", [Object])
|
61 | ], ParseBoolPipe);
|
62 | exports.ParseBoolPipe = ParseBoolPipe;
|