Pipes in NestJS are @Injectable() classes implementing the PipeTransform interface. They interpose between the client request and the route handler, operating on method arguments before the handler executes. They serve two use cases: transformation (convert input data) and validation (evaluate and pass-through or throw).
Pipe lifecycle
Pipes run inside the exceptions zone. When a pipe throws an exception, the exceptions layer handles it and the controller method never executes.
The transform method
Every pipe implements transform(value, metadata) from PipeTransform:
value— the current method argument before the handler receives itmetadata— the ArgumentMetadata describing the argument’s type, metatype, and data
Built-in pipes
Nest provides several built-in pipes from @nestjs/common:
Transformation pipes: ParseIntPipe, ParseFloatPipe, ParseBoolPipe, ParseArrayPipe, ParseUUIDPipe, ParseEnumPipe, ParseDatePipe, ParseFilePipe — convert strings to typed values.
Validation pipe: ValidationPipe — integrates with class-validator and class-transformer for decorator-based DTO validation.
Utility pipe: DefaultValuePipe — supplies fallback values before Parse* pipes handle optional parameters.
Custom pipes
Custom pipes can use schema-based validation (ZodValidationPipe with Zod) or decorator-based validation (ValidationPipe with class-validator). The latter requires classes (not interfaces) for DTOs because class-transformer’s plainToInstance() needs runtime types — the core reason behind DTO Classes vs Interfaces.
Pipe scoping
Pipes can be bound at four levels of Pipe Scoping:
// Parameter-scoped
@Param('id', ParseIntPipe) id: number
// Method-scoped
@UsePipes(new ZodValidationPipe(schema))
// Global-scoped (DI-enabled via APP_PIPE token)
providers: [{ provide: APP_PIPE, useClass: ValidationPipe }]Why pipes instead of middleware for validation
Middleware is unaware of the execution context — it doesn’t know which handler or parameters will be invoked. Pipes have full ArgumentMetadata access, enabling generic validation across contexts.