{"id":"0de8083b86003015","repo":"nestjs/nest","slug":"validation-failed-0de808","errorCode":null,"errorMessage":"Validation failed","messagePattern":"Validation failed","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"sample/01-cats-app/src/common/pipes/validation.pipe.ts","lineNumber":21,"sourceCode":"  BadRequestException,\n  Injectable,\n  PipeTransform,\n  Type,\n} from '@nestjs/common';\nimport { plainToInstance } from 'class-transformer';\nimport { validate } from 'class-validator';\n\n@Injectable()\nexport class ValidationPipe implements PipeTransform<any> {\n  async transform(value: any, metadata: ArgumentMetadata) {\n    const { metatype } = metadata;\n    if (!metatype || !this.toValidate(metatype)) {\n      return value;\n    }\n    const object = plainToInstance(metatype, value);\n    const errors = await validate(object);\n    if (errors.length > 0) {\n      throw new BadRequestException('Validation failed');\n    }\n    return value;\n  }\n\n  private toValidate(metatype: Type<any>): boolean {\n    const types = [String, Boolean, Number, Array, Object];\n    return !types.find(type => metatype === type);\n  }\n}\n","sourceCodeStart":3,"sourceCodeEnd":31,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/sample/01-cats-app/src/common/pipes/validation.pipe.ts#L3-L31","documentation":"Error \"Validation failed\" thrown in nestjs/nest.","triggerScenarios":"Thrown at sample/01-cats-app/src/common/pipes/validation.pipe.ts:21 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Send a request body that satisfies the DTO's class-validator decorators; ValidationPipe throws BadRequestException on constraint failures.","Check the validation errors array in the response for which fields failed and fix the payload accordingly.","Ensure class-transformer can instantiate the metatype (use @Body() with a proper DTO class, not an interface)."],"exampleFix":"// DTO\nexport class CreateCatDto {\n  @IsString() name: string;\n  @IsInt() age: number;\n}\n// client must send { \"name\": \"Tom\", \"age\": 3 }","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}