nestjs/nest · error · BadRequestException
Validation failed
Error message
Validation failed
What it means
Error "Validation failed" thrown in nestjs/nest.
Source
Thrown at sample/10-fastify/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
- Send a request body that passes the DTO's class-validator constraints; the validation pipe throws on failed validation.
- Inspect the error response to see which properties failed and correct the payload.
- Use a DTO class (not an interface) as the @Body() metatype so validation can run.
Example fix
export class CreateCatDto {
@IsString() name: string;
@IsInt() age: number;
}
// POST body: { "name": "Tom", "age": 3 } When it happens
Trigger: Thrown at sample/10-fastify/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/6ebea2ec6df27e85.json.
Report an issue: GitHub.