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

  1. Uses plainToInstance() from class-transformer to transform the plain request body into a typed DTO instance
  2. Uses validate() from class-validator to run decorator-based validations
  3. Skips native JavaScript types (String, Boolean, Number, Array, Object) which can’t have validation decorators
  4. Returns the value unchanged on success, or throws BadRequestException on 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.