|
1 | | -import { BadRequestException, HttpStatus, ValidationPipe, ValidationError, Logger } from '@nestjs/common' |
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + HttpStatus, |
| 4 | + ValidationPipe, |
| 5 | + ValidationError, |
| 6 | + Logger, |
| 7 | + Injectable, |
| 8 | + Scope, Inject, |
| 9 | +} from '@nestjs/common' |
| 10 | +import { REQUEST } from '@nestjs/core' |
| 11 | +import { Request } from 'express' |
2 | 12 |
|
| 13 | +interface ValidationRecursive { |
| 14 | + [key: string]: string |
| 15 | +} |
| 16 | + |
| 17 | +@Injectable({ scope: Scope.REQUEST }) |
3 | 18 | export class DtoValidationPipe extends ValidationPipe { |
4 | | - public constructor() { |
| 19 | + public constructor(@Inject(REQUEST) protected readonly request: Request) { |
5 | 20 | super({ |
6 | 21 | transform: true, |
7 | | - exceptionFactory: (errors) => { |
8 | | - const validations = {} |
9 | | - const errorsMessage = [] |
10 | | - errors.forEach((error: ValidationError) => { |
11 | | - if (error.constraints) { |
12 | | - Object.values(error.constraints).forEach((value) => { |
13 | | - validations[error.property] = value |
14 | | - errorsMessage.push(`${error.property}: ${value}`) |
15 | | - }) |
16 | | - } |
17 | | - if (error.children.length > 0) { |
18 | | - validations[error.property] = {} |
19 | | - error.children.forEach((childError: ValidationError) => { |
20 | | - if (childError.constraints) { |
21 | | - Object.values(childError.constraints).forEach((value) => { |
22 | | - validations[error.property][childError.property] = value |
23 | | - errorsMessage.push(`${error.property}.${childError.property}: ${value}`) |
24 | | - }) |
25 | | - } |
26 | | - }) |
27 | | - } |
28 | | - }) |
29 | | - const message = `Validation failed: ${errorsMessage.join(', ')}` |
30 | | - Logger.debug(message, 'DtoValidationPipe') |
| 22 | + transformOptions: { |
| 23 | + enableImplicitConversion: true, |
| 24 | + }, |
| 25 | + exceptionFactory: (errors: ValidationError[]) => { |
| 26 | + let validations: ValidationRecursive = {} |
| 27 | + for (const error of errors) { |
| 28 | + validations = { ...validations, ...this.validationRecursive(error)} |
| 29 | + } |
| 30 | + const debug = {} |
| 31 | + const message = `Erreur de validation : ${Object.keys(validations).join(', ')}`.trim() |
| 32 | + Logger.debug(`${message} (${JSON.stringify(validations)})`, 'DtoValidationPipe') |
| 33 | + if (process.env.NODE_ENV !== 'production' && request.query['debug']) { |
| 34 | + debug['_errors'] = errors |
| 35 | + } |
31 | 36 | return new BadRequestException({ |
32 | 37 | statusCode: HttpStatus.BAD_REQUEST, |
33 | 38 | message, |
34 | 39 | validations, |
| 40 | + ...debug, |
35 | 41 | }) |
36 | 42 | }, |
37 | 43 | }) |
38 | 44 | } |
| 45 | + |
| 46 | + public validationRecursive(error: ValidationError, prefix = ''): ValidationRecursive { |
| 47 | + let validations = {} |
| 48 | + if (error.constraints) { |
| 49 | + validations[`${prefix + error.property}`] = Object.values(error.constraints)[0] |
| 50 | + } |
| 51 | + if (error.children.length > 0) { |
| 52 | + for (const errorChild of error.children) { |
| 53 | + if (errorChild.constraints) { |
| 54 | + validations[`${prefix + errorChild.property}`] = Object.values(errorChild.constraints)[0] |
| 55 | + } |
| 56 | + if (errorChild.children.length > 0) { |
| 57 | + validations = { ...validations, ...this.validationRecursive(errorChild, `${prefix + error.property}.${errorChild.property}.`) } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return validations |
| 62 | + } |
39 | 63 | } |
0 commit comments