UNPKG

2.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParseBoolPipe = void 0;
4const tslib_1 = require("tslib");
5const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
6const optional_decorator_1 = require("../decorators/core/optional.decorator");
7const http_status_enum_1 = require("../enums/http-status.enum");
8const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
9/**
10 * Defines the built-in ParseBool Pipe
11 *
12 * @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
13 *
14 * @publicApi
15 */
16let 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 * Method that accesses and performs optional transformation on argument for
26 * in-flight requests.
27 *
28 * @param value currently processed route argument
29 * @param metadata contains metadata about the currently processed route argument
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 * @param value currently processed route argument
42 * @returns `true` if `value` is said 'true', ie., if it is equal to the boolean
43 * `true` or the string `"true"`
44 */
45 isTrue(value) {
46 return value === true || value === 'true';
47 }
48 /**
49 * @param value currently processed route argument
50 * @returns `true` if `value` is said 'false', ie., if it is equal to the boolean
51 * `false` or the string `"false"`
52 */
53 isFalse(value) {
54 return value === false || value === 'false';
55 }
56};
57ParseBoolPipe = 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);
62exports.ParseBoolPipe = ParseBoolPipe;