How to Use @UsePipes in NestJS: Simple Guide with Examples
In NestJS, use the
@UsePipes decorator to apply pipes like validation or transformation to route handlers or controllers. You place @UsePipes above methods or classes to automatically process incoming data with the specified pipes.Syntax
The @UsePipes decorator takes one or more pipe instances as arguments and applies them to the decorated method or class. Pipes process incoming request data before it reaches the handler.
Example parts:
@UsePipes(pipe1, pipe2): Apply one or multiple pipes.- Placed above a controller method or class.
- Pipes must implement the
PipeTransforminterface.
typescript
import { UsePipes, PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() class MyPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { // transform or validate value return value; } } @UsePipes(new MyPipe()) class MyController { myMethod() { // handler code } }
Example
This example shows how to use @UsePipes with NestJS's built-in ValidationPipe to validate incoming data in a POST request.
typescript
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common'; import { IsString, Length } from 'class-validator'; class CreateUserDto { @IsString() @Length(3, 20) username: string; } @Controller('users') export class UsersController { @Post() @UsePipes(new ValidationPipe()) createUser(@Body() createUserDto: CreateUserDto) { return { message: `User ${createUserDto.username} created` }; } }
Output
POST /users with body {"username": "Jo"} returns 400 Bad Request (validation error)
POST /users with body {"username": "JohnDoe"} returns {"message": "User JohnDoe created"}
Common Pitfalls
Common mistakes when using @UsePipes include:
- Not instantiating the pipe (e.g., passing the class instead of
new Pipe()). - Applying
@UsePipesat the wrong level (method vs class) leading to unexpected behavior. - Forgetting to enable validation decorators in DTOs when using
ValidationPipe.
Always create pipe instances and ensure DTOs have proper validation rules.
typescript
import { UsePipes, ValidationPipe } from '@nestjs/common'; // Wrong: passing class instead of instance @UsePipes(ValidationPipe) myMethod() {} // Right: pass an instance @UsePipes(new ValidationPipe()) myMethod() {}
Quick Reference
| Usage | Description |
|---|---|
| @UsePipes(pipeInstance) | Apply one or more pipes to a method or controller |
| new ValidationPipe() | Built-in pipe to validate DTOs using class-validator |
| Custom pipes | Create by implementing PipeTransform interface |
| Apply at method level | Pipe affects only that route handler |
| Apply at controller level | Pipe affects all routes in the controller |
Key Takeaways
Use @UsePipes with pipe instances to apply validation or transformation in NestJS.
Place @UsePipes above methods or controllers depending on scope needed.
Always instantiate pipes with new before passing to @UsePipes.
Use built-in ValidationPipe with DTOs for easy request data validation.
Custom pipes must implement PipeTransform interface with a transform method.