nestjs/nest · error · BadRequestException

Validation failed

Error message

Validation failed

What it means

Error "Validation failed" thrown in nestjs/nest.

Source

Thrown at sample/01-cats-app/src/common/pipes/validation.pipe.ts:21

  BadRequestException,
  Injectable,
  PipeTransform,
  Type,
} from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

@Injectable()
export class ValidationPipe implements PipeTransform<any> {
  async transform(value: any, metadata: ArgumentMetadata) {
    const { metatype } = metadata;
    if (!metatype || !this.toValidate(metatype)) {
      return value;
    }
    const object = plainToInstance(metatype, value);
    const errors = await validate(object);
    if (errors.length > 0) {
      throw new BadRequestException('Validation failed');
    }
    return value;
  }

  private toValidate(metatype: Type<any>): boolean {
    const types = [String, Boolean, Number, Array, Object];
    return !types.find(type => metatype === type);
  }
}

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Send a request body that satisfies the DTO's class-validator decorators; ValidationPipe throws BadRequestException on constraint failures.
  2. Check the validation errors array in the response for which fields failed and fix the payload accordingly.
  3. Ensure class-transformer can instantiate the metatype (use @Body() with a proper DTO class, not an interface).

Example fix

// DTO
export class CreateCatDto {
  @IsString() name: string;
  @IsInt() age: number;
}
// client must send { "name": "Tom", "age": 3 }

When it happens

Trigger: Thrown at sample/01-cats-app/src/common/pipes/validation.pipe.ts:21 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of nestjs/nest@6ec0e2783d (2026-08-03). Data as JSON: /data/errors/0de8083b86003015.json. Report an issue: GitHub.