UNPKG

8.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ValidationPipe = void 0;
4const tslib_1 = require("tslib");
5const iterare_1 = require("iterare");
6const util_1 = require("util");
7const decorators_1 = require("../decorators");
8const core_1 = require("../decorators/core");
9const http_status_enum_1 = require("../enums/http-status.enum");
10const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
11const load_package_util_1 = require("../utils/load-package.util");
12const shared_utils_1 = require("../utils/shared.utils");
13let classValidator = {};
14let classTransformer = {};
15/**
16 * @see [Validation](https://docs.nestjs.com/techniques/validation)
17 *
18 * @publicApi
19 */
20let ValidationPipe = class ValidationPipe {
21 constructor(options) {
22 options = options || {};
23 const { transform, disableErrorMessages, errorHttpStatusCode, expectedType, transformOptions, validateCustomDecorators, ...validatorOptions } = options;
24 // @see https://github.com/nestjs/nest/issues/10683#issuecomment-1413690508
25 this.validatorOptions = { forbidUnknownValues: false, ...validatorOptions };
26 this.isTransformEnabled = !!transform;
27 this.transformOptions = transformOptions;
28 this.isDetailedOutputDisabled = disableErrorMessages;
29 this.validateCustomDecorators = validateCustomDecorators || false;
30 this.errorHttpStatusCode = errorHttpStatusCode || http_status_enum_1.HttpStatus.BAD_REQUEST;
31 this.expectedType = expectedType;
32 this.exceptionFactory =
33 options.exceptionFactory || this.createExceptionFactory();
34 classValidator = this.loadValidator(options.validatorPackage);
35 classTransformer = this.loadTransformer(options.transformerPackage);
36 }
37 loadValidator(validatorPackage) {
38 return (validatorPackage ??
39 (0, load_package_util_1.loadPackage)('class-validator', 'ValidationPipe', () => require('class-validator')));
40 }
41 loadTransformer(transformerPackage) {
42 return (transformerPackage ??
43 (0, load_package_util_1.loadPackage)('class-transformer', 'ValidationPipe', () => require('class-transformer')));
44 }
45 async transform(value, metadata) {
46 if (this.expectedType) {
47 metadata = { ...metadata, metatype: this.expectedType };
48 }
49 const metatype = metadata.metatype;
50 if (!metatype || !this.toValidate(metadata)) {
51 return this.isTransformEnabled
52 ? this.transformPrimitive(value, metadata)
53 : value;
54 }
55 const originalValue = value;
56 value = this.toEmptyIfNil(value);
57 const isNil = value !== originalValue;
58 const isPrimitive = this.isPrimitive(value);
59 this.stripProtoKeys(value);
60 let entity = classTransformer.plainToClass(metatype, value, this.transformOptions);
61 const originalEntity = entity;
62 const isCtorNotEqual = entity.constructor !== metatype;
63 if (isCtorNotEqual && !isPrimitive) {
64 entity.constructor = metatype;
65 }
66 else if (isCtorNotEqual) {
67 // when "entity" is a primitive value, we have to temporarily
68 // replace the entity to perform the validation against the original
69 // metatype defined inside the handler
70 entity = { constructor: metatype };
71 }
72 const errors = await this.validate(entity, this.validatorOptions);
73 if (errors.length > 0) {
74 throw await this.exceptionFactory(errors);
75 }
76 if (isPrimitive) {
77 // if the value is a primitive value and the validation process has been successfully completed
78 // we have to revert the original value passed through the pipe
79 entity = originalEntity;
80 }
81 if (this.isTransformEnabled) {
82 return entity;
83 }
84 if (isNil) {
85 // if the value was originally undefined or null, revert it back
86 return originalValue;
87 }
88 // we check if the number of keys of the "validatorOptions" is higher than 1 (instead of 0)
89 // because the "forbidUnknownValues" now fallbacks to "false" (in case it wasn't explicitly specified)
90 const shouldTransformToPlain = Object.keys(this.validatorOptions).length > 1;
91 return shouldTransformToPlain
92 ? classTransformer.classToPlain(entity, this.transformOptions)
93 : value;
94 }
95 createExceptionFactory() {
96 return (validationErrors = []) => {
97 if (this.isDetailedOutputDisabled) {
98 return new http_error_by_code_util_1.HttpErrorByCode[this.errorHttpStatusCode]();
99 }
100 const errors = this.flattenValidationErrors(validationErrors);
101 return new http_error_by_code_util_1.HttpErrorByCode[this.errorHttpStatusCode](errors);
102 };
103 }
104 toValidate(metadata) {
105 const { metatype, type } = metadata;
106 if (type === 'custom' && !this.validateCustomDecorators) {
107 return false;
108 }
109 const types = [String, Boolean, Number, Array, Object, Buffer, Date];
110 return !types.some(t => metatype === t) && !(0, shared_utils_1.isNil)(metatype);
111 }
112 transformPrimitive(value, metadata) {
113 if (!metadata.data) {
114 // leave top-level query/param objects unmodified
115 return value;
116 }
117 const { type, metatype } = metadata;
118 if (type !== 'param' && type !== 'query') {
119 return value;
120 }
121 if (metatype === Boolean) {
122 if ((0, shared_utils_1.isUndefined)(value)) {
123 // This is an workaround to deal with optional boolean values since
124 // optional booleans shouldn't be parsed to a valid boolean when
125 // they were not defined
126 return undefined;
127 }
128 // Any fasly value but `undefined` will be parsed to `false`
129 return value === true || value === 'true';
130 }
131 if (metatype === Number) {
132 return +value;
133 }
134 return value;
135 }
136 toEmptyIfNil(value) {
137 return (0, shared_utils_1.isNil)(value) ? {} : value;
138 }
139 stripProtoKeys(value) {
140 if (value == null ||
141 typeof value !== 'object' ||
142 util_1.types.isTypedArray(value)) {
143 return;
144 }
145 if (Array.isArray(value)) {
146 for (const v of value) {
147 this.stripProtoKeys(v);
148 }
149 return;
150 }
151 delete value.__proto__;
152 for (const key in value) {
153 this.stripProtoKeys(value[key]);
154 }
155 }
156 isPrimitive(value) {
157 return ['number', 'boolean', 'string'].includes(typeof value);
158 }
159 validate(object, validatorOptions) {
160 return classValidator.validate(object, validatorOptions);
161 }
162 flattenValidationErrors(validationErrors) {
163 return (0, iterare_1.iterate)(validationErrors)
164 .map(error => this.mapChildrenToValidationErrors(error))
165 .flatten()
166 .filter(item => !!item.constraints)
167 .map(item => Object.values(item.constraints))
168 .flatten()
169 .toArray();
170 }
171 mapChildrenToValidationErrors(error, parentPath) {
172 if (!(error.children && error.children.length)) {
173 return [error];
174 }
175 const validationErrors = [];
176 parentPath = parentPath
177 ? `${parentPath}.${error.property}`
178 : error.property;
179 for (const item of error.children) {
180 if (item.children && item.children.length) {
181 validationErrors.push(...this.mapChildrenToValidationErrors(item, parentPath));
182 }
183 validationErrors.push(this.prependConstraintsWithParentProp(parentPath, item));
184 }
185 return validationErrors;
186 }
187 prependConstraintsWithParentProp(parentPath, error) {
188 const constraints = {};
189 for (const key in error.constraints) {
190 constraints[key] = `${parentPath}.${error.constraints[key]}`;
191 }
192 return {
193 ...error,
194 constraints,
195 };
196 }
197};
198exports.ValidationPipe = ValidationPipe;
199exports.ValidationPipe = ValidationPipe = tslib_1.__decorate([
200 (0, core_1.Injectable)(),
201 tslib_1.__param(0, (0, decorators_1.Optional)()),
202 tslib_1.__metadata("design:paramtypes", [Object])
203], ValidationPipe);