|
| 1 | +import { |
| 2 | + ArgumentMetadata, |
| 3 | + Injectable, |
| 4 | + Paramtype, |
| 5 | + PipeTransform, |
| 6 | + Type, |
| 7 | + UnprocessableEntityException, |
| 8 | +} from '@nestjs/common'; |
| 9 | +import { plainToInstance } from 'class-transformer'; |
| 10 | +import { validate, ValidationError } from 'class-validator'; |
| 11 | +import { StdResponse } from '@common/std-response/std-response'; |
| 12 | +import { Result } from '@common/result/result'; |
| 13 | +import { ValidationFailure } from './validation-failure'; |
| 14 | +import { ErrorCode } from '@common/result/error'; |
| 15 | + |
| 16 | +@Injectable() |
| 17 | +export class ValidationPipe implements PipeTransform { |
| 18 | + constructor( |
| 19 | + private readonly types: Paramtype[], |
| 20 | + private readonly transport: 'http' | 'ws', |
| 21 | + ) {} |
| 22 | + |
| 23 | + async transform(value: any, metadata: ArgumentMetadata) { |
| 24 | + if ( |
| 25 | + !this.types.includes(metadata.type) || |
| 26 | + !metadata.metatype || |
| 27 | + !this.toValidate(metadata.metatype) |
| 28 | + ) { |
| 29 | + return value; |
| 30 | + } |
| 31 | + |
| 32 | + let oldValue = value; |
| 33 | + let ack = null; |
| 34 | + if (this.transport === 'ws') { |
| 35 | + oldValue = value.data; |
| 36 | + ack = value.ack; |
| 37 | + } else if (this.transport === 'http' && metadata.type === 'param') { |
| 38 | + // For params, I wrap the single param value in an object |
| 39 | + oldValue = { [metadata.data]: value }; |
| 40 | + } |
| 41 | + |
| 42 | + const newValue = plainToInstance(metadata.metatype, oldValue); |
| 43 | + const errors = await validate(newValue); |
| 44 | + |
| 45 | + if (errors.length > 0) { |
| 46 | + if (this.transport === 'ws' && ack) { |
| 47 | + ack( |
| 48 | + StdResponse.fromResult<ValidationFailure[]>( |
| 49 | + this.formatErrors(errors), |
| 50 | + ), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (this.transport === 'http') { |
| 55 | + throw new UnprocessableEntityException( |
| 56 | + StdResponse.fromResult(this.formatErrors(errors)), |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return value; |
| 62 | + } |
| 63 | + |
| 64 | + private toValidate(metatype: Type<any>): boolean { |
| 65 | + const types = [String, Boolean, Number, Array, Object]; |
| 66 | + return !types.includes(metatype as any); |
| 67 | + } |
| 68 | + |
| 69 | + private formatErrors(validationErrors: ValidationError[]): Result<any, any> { |
| 70 | + const errors: ValidationFailure[] = []; |
| 71 | + |
| 72 | + // Transform validation errors into ValidationFailure instances and push them to the array. |
| 73 | + validationErrors.forEach((error) => { |
| 74 | + if (error.constraints) { |
| 75 | + for (const failure of Object.values(error.constraints)) { |
| 76 | + errors.push(new ValidationFailure(error.property, failure)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (error.children) { |
| 81 | + for (const subError of error.children) { |
| 82 | + errors.push( |
| 83 | + new ValidationFailure(subError.property, error.property + '.'), |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return errors; |
| 89 | + }); |
| 90 | + |
| 91 | + return Result.error( |
| 92 | + 'Validation Error', |
| 93 | + ErrorCode.VALIDATION_FAILURE, |
| 94 | + errors, |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments