• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nestjs / nest / 0ab3929a-a508-4077-a49d-88827d5b3ed1

03 Jan 2025 06:04PM UTC coverage: 91.915% (+0.003%) from 91.912%
0ab3929a-a508-4077-a49d-88827d5b3ed1

Pull #14359

circleci

civilcoder55
feat(common): add new option disableFlattenErrorMessages to validation pipe
Pull Request #14359: feat(common): add new option disableFlattenErrorMessages to validation pipe

2088 of 2526 branches covered (82.66%)

5 of 5 new or added lines in 1 file covered. (100.0%)

9 existing lines in 1 file now uncovered.

6821 of 7421 relevant lines covered (91.91%)

17.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/packages/common/pipes/validation.pipe.ts
1
import { iterate } from 'iterare';
1✔
2
import { types } from 'util';
1✔
3
import { Optional } from '../decorators';
1✔
4
import { Injectable } from '../decorators/core';
1✔
5
import { HttpStatus } from '../enums/http-status.enum';
1✔
6
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
7
import { TransformerPackage } from '../interfaces/external/transformer-package.interface';
8
import { ValidationError } from '../interfaces/external/validation-error.interface';
9
import { ValidatorOptions } from '../interfaces/external/validator-options.interface';
10
import { ValidatorPackage } from '../interfaces/external/validator-package.interface';
11
import {
12
  ArgumentMetadata,
13
  PipeTransform,
14
} from '../interfaces/features/pipe-transform.interface';
15
import { Type } from '../interfaces/type.interface';
16
import {
1✔
17
  ErrorHttpStatusCode,
18
  HttpErrorByCode,
19
} from '../utils/http-error-by-code.util';
20
import { loadPackage } from '../utils/load-package.util';
1✔
21
import { isNil, isUndefined } from '../utils/shared.utils';
1✔
22

23
/**
24
 * @publicApi
25
 */
26
export interface ValidationPipeOptions extends ValidatorOptions {
27
  transform?: boolean;
28
  disableErrorMessages?: boolean;
29
  disableFlattenErrorMessages?: boolean;
30
  transformOptions?: ClassTransformOptions;
31
  errorHttpStatusCode?: ErrorHttpStatusCode;
32
  exceptionFactory?: (errors: ValidationError[] | string[]) => any;
33
  validateCustomDecorators?: boolean;
34
  expectedType?: Type<any>;
35
  validatorPackage?: ValidatorPackage;
36
  transformerPackage?: TransformerPackage;
37
}
38

39
let classValidator: ValidatorPackage = {} as any;
1✔
40
let classTransformer: TransformerPackage = {} as any;
1✔
41

42
/**
43
 * @see [Validation](https://docs.nestjs.com/techniques/validation)
44
 *
45
 * @publicApi
46
 */
47
@Injectable()
48
export class ValidationPipe implements PipeTransform<any> {
1✔
49
  protected isTransformEnabled: boolean;
50
  protected isDetailedOutputDisabled?: boolean;
51
  protected isFlattenErrorMessagesDisabled?: boolean;
52
  protected validatorOptions: ValidatorOptions;
53
  protected transformOptions: ClassTransformOptions;
54
  protected errorHttpStatusCode: ErrorHttpStatusCode;
55
  protected expectedType: Type<any>;
56
  protected exceptionFactory: (errors: ValidationError[] | string[]) => any;
57
  protected validateCustomDecorators: boolean;
58

59
  constructor(@Optional() options?: ValidationPipeOptions) {
60
    options = options || {};
60✔
61
    const {
62
      transform,
63
      disableErrorMessages,
64
      disableFlattenErrorMessages,
65
      errorHttpStatusCode,
66
      expectedType,
67
      transformOptions,
68
      validateCustomDecorators,
69
      ...validatorOptions
70
    } = options;
60✔
71

72
    // @see https://github.com/nestjs/nest/issues/10683#issuecomment-1413690508
73
    this.validatorOptions = { forbidUnknownValues: false, ...validatorOptions };
60✔
74

75
    this.isTransformEnabled = !!transform;
60✔
76
    this.transformOptions = transformOptions;
60✔
77
    this.isDetailedOutputDisabled = disableErrorMessages;
60✔
78
    this.isFlattenErrorMessagesDisabled = disableFlattenErrorMessages;
60✔
79
    this.validateCustomDecorators = validateCustomDecorators || false;
60✔
80
    this.errorHttpStatusCode = errorHttpStatusCode || HttpStatus.BAD_REQUEST;
60✔
81
    this.expectedType = expectedType;
60✔
82
    this.exceptionFactory =
60✔
83
      options.exceptionFactory || this.createExceptionFactory();
116✔
84

85
    classValidator = this.loadValidator(options.validatorPackage);
60✔
86
    classTransformer = this.loadTransformer(options.transformerPackage);
60✔
87
  }
88

89
  protected loadValidator(
90
    validatorPackage?: ValidatorPackage,
91
  ): ValidatorPackage {
92
    return (
60✔
93
      validatorPackage ??
120✔
94
      loadPackage('class-validator', 'ValidationPipe', () =>
95
        require('class-validator'),
60✔
96
      )
97
    );
98
  }
99

100
  protected loadTransformer(
101
    transformerPackage?: TransformerPackage,
102
  ): TransformerPackage {
103
    return (
60✔
104
      transformerPackage ??
120✔
105
      loadPackage('class-transformer', 'ValidationPipe', () =>
106
        require('class-transformer'),
60✔
107
      )
108
    );
109
  }
110

111
  public async transform(value: any, metadata: ArgumentMetadata) {
112
    if (this.expectedType) {
63✔
113
      metadata = { ...metadata, metatype: this.expectedType };
2✔
114
    }
115

116
    const metatype = metadata.metatype;
63✔
117
    if (!metatype || !this.toValidate(metadata)) {
63✔
118
      return this.isTransformEnabled
16✔
119
        ? this.transformPrimitive(value, metadata)
120
        : value;
121
    }
122
    const originalValue = value;
47✔
123
    value = this.toEmptyIfNil(value, metatype);
47✔
124

125
    const isNil = value !== originalValue;
47✔
126
    const isPrimitive = this.isPrimitive(value);
47✔
127
    this.stripProtoKeys(value);
47✔
128

129
    let entity = classTransformer.plainToClass(
47✔
130
      metatype,
131
      value,
132
      this.transformOptions,
133
    );
134

135
    const originalEntity = entity;
47✔
136
    const isCtorNotEqual = entity.constructor !== metatype;
47✔
137

138
    if (isCtorNotEqual && !isPrimitive) {
47✔
139
      entity.constructor = metatype;
2✔
140
    } else if (isCtorNotEqual) {
45✔
141
      // when "entity" is a primitive value, we have to temporarily
142
      // replace the entity to perform the validation against the original
143
      // metatype defined inside the handler
144
      entity = { constructor: metatype };
6✔
145
    }
146

147
    const errors = await this.validate(entity, this.validatorOptions);
47✔
148
    if (errors.length > 0) {
47✔
149
      let validationErrors: ValidationError[] | string[] = errors;
20✔
150
      if (!this.isFlattenErrorMessagesDisabled) {
20✔
151
        validationErrors = this.flattenValidationErrors(errors);
19✔
152
      }
153
      throw await this.exceptionFactory(validationErrors);
20✔
154
    }
155
    if (isPrimitive) {
27✔
156
      // if the value is a primitive value and the validation process has been successfully completed
157
      // we have to revert the original value passed through the pipe
158
      entity = originalEntity;
3✔
159
    }
160
    if (this.isTransformEnabled) {
27✔
161
      return entity;
10✔
162
    }
163
    if (isNil) {
17!
164
      // if the value was originally undefined or null, revert it back
UNCOV
165
      return originalValue;
×
166
    }
167
    // we check if the number of keys of the "validatorOptions" is higher than 1 (instead of 0)
168
    // because the "forbidUnknownValues" now fallbacks to "false" (in case it wasn't explicitly specified)
169
    const shouldTransformToPlain =
170
      Object.keys(this.validatorOptions).length > 1;
17✔
171
    return shouldTransformToPlain
17✔
172
      ? classTransformer.classToPlain(entity, this.transformOptions)
173
      : value;
174
  }
175

176
  public createExceptionFactory() {
177
    return (validationErrors: ValidationError[] | string[] = []) => {
56!
178
      if (this.isDetailedOutputDisabled) {
16!
UNCOV
179
        return new HttpErrorByCode[this.errorHttpStatusCode]();
×
180
      }
181
      return new HttpErrorByCode[this.errorHttpStatusCode](validationErrors);
16✔
182
    };
183
  }
184

185
  protected toValidate(metadata: ArgumentMetadata): boolean {
186
    const { metatype, type } = metadata;
59✔
187
    if (type === 'custom' && !this.validateCustomDecorators) {
59✔
188
      return false;
2✔
189
    }
190
    const types = [String, Boolean, Number, Array, Object, Buffer, Date];
57✔
191
    return !types.some(t => metatype === t) && !isNil(metatype);
351✔
192
  }
193

194
  protected transformPrimitive(value: any, metadata: ArgumentMetadata) {
195
    if (!metadata.data) {
10!
196
      // leave top-level query/param objects unmodified
UNCOV
197
      return value;
×
198
    }
199
    const { type, metatype } = metadata;
10✔
200
    if (type !== 'param' && type !== 'query') {
10!
UNCOV
201
      return value;
×
202
    }
203
    if (metatype === Boolean) {
10✔
204
      if (isUndefined(value)) {
8✔
205
        // This is an workaround to deal with optional boolean values since
206
        // optional booleans shouldn't be parsed to a valid boolean when
207
        // they were not defined
208
        return undefined;
2✔
209
      }
210
      // Any fasly value but `undefined` will be parsed to `false`
211
      return value === true || value === 'true';
6✔
212
    }
213
    if (metatype === Number) {
2✔
214
      return +value;
2✔
215
    }
UNCOV
216
    if (metatype === String && !isUndefined(value)) {
×
UNCOV
217
      return String(value);
×
218
    }
UNCOV
219
    return value;
×
220
  }
221

222
  protected toEmptyIfNil<T = any, R = any>(
223
    value: T,
224
    metatype: Type<unknown> | object,
225
  ): R | {} {
226
    if (!isNil(value)) {
47✔
227
      return value;
47✔
228
    }
UNCOV
229
    if (
×
230
      typeof metatype === 'function' ||
×
231
      (metatype && 'prototype' in metatype && metatype.prototype?.constructor)
232
    ) {
UNCOV
233
      return {};
×
234
    }
235
    // Builder like SWC require empty string to be returned instead of an empty object
236
    // when the value is nil and the metatype is not a class instance, but a plain object (enum, for example).
237
    // Otherwise, the error will be thrown.
238
    // @see https://github.com/nestjs/nest/issues/12680
239
    return '';
×
240
  }
241

242
  protected stripProtoKeys(value: any) {
243
    if (
140✔
244
      value == null ||
332✔
245
      typeof value !== 'object' ||
246
      types.isTypedArray(value)
247
    ) {
248
      return;
84✔
249
    }
250
    if (Array.isArray(value)) {
56✔
251
      for (const v of value) {
5✔
252
        this.stripProtoKeys(v);
6✔
253
      }
254
      return;
5✔
255
    }
256
    delete value.__proto__;
51✔
257
    for (const key in value) {
51✔
258
      this.stripProtoKeys(value[key]);
87✔
259
    }
260
  }
261

262
  protected isPrimitive(value: unknown): boolean {
263
    return ['number', 'boolean', 'string'].includes(typeof value);
47✔
264
  }
265

266
  protected validate(
267
    object: object,
268
    validatorOptions?: ValidatorOptions,
269
  ): Promise<ValidationError[]> | ValidationError[] {
270
    return classValidator.validate(object, validatorOptions);
47✔
271
  }
272

273
  protected flattenValidationErrors(
274
    validationErrors: ValidationError[],
275
  ): string[] {
276
    return iterate(validationErrors)
19✔
277
      .map(error => this.mapChildrenToValidationErrors(error))
25✔
278
      .flatten()
279
      .filter(item => !!item.constraints)
34✔
280
      .map(item => Object.values(item.constraints))
34✔
281
      .flatten()
282
      .toArray();
283
  }
284

285
  protected mapChildrenToValidationErrors(
286
    error: ValidationError,
287
    parentPath?: string,
288
  ): ValidationError[] {
289
    if (!(error.children && error.children.length)) {
29✔
290
      return [error];
19✔
291
    }
292
    const validationErrors = [];
10✔
293
    parentPath = parentPath
10✔
294
      ? `${parentPath}.${error.property}`
295
      : error.property;
296
    for (const item of error.children) {
10✔
297
      if (item.children && item.children.length) {
15✔
298
        validationErrors.push(
4✔
299
          ...this.mapChildrenToValidationErrors(item, parentPath),
300
        );
301
      }
302
      validationErrors.push(
15✔
303
        this.prependConstraintsWithParentProp(parentPath, item),
304
      );
305
    }
306
    return validationErrors;
10✔
307
  }
308

309
  protected prependConstraintsWithParentProp(
310
    parentPath: string,
311
    error: ValidationError,
312
  ): ValidationError {
313
    const constraints = {};
15✔
314
    for (const key in error.constraints) {
15✔
315
      constraints[key] = `${parentPath}.${error.constraints[key]}`;
13✔
316
    }
317
    return {
15✔
318
      ...error,
319
      constraints,
320
    };
321
  }
322
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc