ValidationPipe is a built-in NestJS pipe that provides automatic validation using the class-validator and class-transformer libraries. It is exported from @nestjs/common.
How it works
- Uses
plainToInstance()from class-transformer to transform the plain request body into a typed DTO instance - Uses
validate()from class-validator to run decorator-based validations - Skips native JavaScript types (String, Boolean, Number, Array, Object) which can’t have validation decorators
- Returns the value unchanged on success, or throws
BadRequestExceptionon failure
Usage
// Parameter-scoped
@Post()
async create(@Body(new ValidationPipe()) dto: CreateCatDto) {}
// Global with DI support
@Module({
providers: [{ provide: APP_PIPE, useClass: ValidationPipe }],
})
export class AppModule {}The built-in ValidationPipe supports async validations, transformation options, whitelisting, and more — beyond the basic example in the docs.