Pipe Binding in NestJS: Parameter, Method, Controller, and Global
📖 Scenario: You are building a simple NestJS API that accepts user input and validates it using pipes. Pipes help transform and validate data before it reaches your route handlers.In this project, you will learn how to bind pipes at different levels: parameter, method, controller, and globally.
🎯 Goal: Build a NestJS controller with a route that uses pipes to validate and transform input at four levels:Parameter level pipeMethod level pipeController level pipeGlobal level pipeThis will help you understand how NestJS applies pipes in different scopes.
📋 What You'll Learn
Create a custom pipe called ParseIntPipe that converts a string to an integer.
Bind ParseIntPipe to a route parameter to transform the input.
Bind ParseIntPipe to a method to transform all parameters of that method.
Bind ParseIntPipe to a controller to transform all routes inside it.
Bind ParseIntPipe globally to transform all incoming requests.
Use NestJS decorators @Param(), @UsePipes(), and app.useGlobalPipes() correctly.
💡 Why This Matters
🌍 Real World
Pipes in NestJS are used to validate and transform incoming data before it reaches your business logic. This helps keep your code clean and secure.
💼 Career
Understanding pipe binding is essential for backend developers working with NestJS to ensure data integrity and proper request handling.
Progress0 / 4 steps
1
Create a custom pipe called ParseIntPipe
Create a class called ParseIntPipe that implements PipeTransform from @nestjs/common. Inside, write a transform method that takes a value parameter and returns parseInt(value, 10).
NestJS
Hint
Remember to import PipeTransform and Injectable from @nestjs/common. The transform method is required for pipes.
2
Bind ParseIntPipe to a route parameter
Create a controller class called AppController with a method getById that takes a parameter id decorated with @Param('id', ParseIntPipe). The method should return the id. Import Controller, Get, and Param from @nestjs/common.
NestJS
Hint
Use @Param('id', ParseIntPipe) to bind the pipe to the id parameter.
3
Bind ParseIntPipe to a method
In AppController, add a new method getByIdWithMethodPipe with a @UsePipes(ParseIntPipe) decorator. The method should take @Param('id') id: number and return the id. Import UsePipes from @nestjs/common.
NestJS
Hint
Use @UsePipes(ParseIntPipe) above the method to apply the pipe to all parameters of that method.
4
Bind ParseIntPipe globally and to the controller
Bind ParseIntPipe to the entire AppController using @UsePipes(ParseIntPipe) above the class. Then, in main.ts, bind ParseIntPipe globally using app.useGlobalPipes(new ParseIntPipe()). Import NestFactory from @nestjs/core and AppModule from your app module file.
NestJS
Hint
Use @UsePipes(ParseIntPipe) above the controller class to apply the pipe to all routes inside it. Use app.useGlobalPipes(new ParseIntPipe()) in main.ts to apply the pipe globally.
Practice
(1/5)
1. What is the main purpose of binding a pipe to a parameter in NestJS?
easy
A. To define routing paths for the controller methods
B. To apply middleware logic before the controller handles the request
C. To globally change all incoming requests in the application
D. To transform or validate the specific parameter before the method uses it
Solution
Step 1: Understand parameter-level pipe binding
Binding a pipe to a parameter means the pipe runs only on that parameter to transform or validate it before the method uses it.
Step 2: Differentiate from other bindings
Middleware and global pipes affect broader scopes, not just a single parameter. Routing is unrelated to pipes.
Final Answer:
To transform or validate the specific parameter before the method uses it -> Option D
A. ValidationPipe should be instantiated with 'new' keyword in @UsePipes
B. @UsePipes cannot be used on methods, only on controllers
C. Query decorator is missing a pipe binding
D. Controller decorator is missing parameters
Solution
Step 1: Check @UsePipes decorator usage
@UsePipes expects an instance of a pipe, so ValidationPipe must be called with 'new'.
Step 2: Validate other parts
@UsePipes can be used on methods, Query decorator is valid, Controller decorator is correct.
Final Answer:
ValidationPipe should be instantiated with 'new' keyword in @UsePipes -> Option A
Quick Check:
@UsePipes needs pipe instances [OK]
Hint: Always instantiate pipes with 'new' in decorators [OK]
Common Mistakes:
Passing pipe class instead of instance
Thinking @UsePipes only works on controllers
Missing pipe on parameter is not an error
5. You want to apply a custom pipe that trims whitespace from all string inputs in a controller and also validate numbers globally. Which binding strategy is best to achieve this cleanly?
hard
A. Bind the trim pipe at controller level and the number validation pipe globally
B. Bind both pipes globally to affect all controllers and methods
C. Bind both pipes only on method parameters where needed
D. Bind the trim pipe globally and the number validation pipe at method level
Solution
Step 1: Understand scope of pipes for separation of concerns
Trimming strings is specific to this controller's inputs, so controller-level binding keeps it scoped and clean.
Step 2: Apply number validation globally
Number validation is common across the app, so global binding ensures consistent validation everywhere.
Final Answer:
Bind the trim pipe at controller level and the number validation pipe globally -> Option A
Quick Check:
Controller pipe for specific, global pipe for common validation [OK]
Hint: Use controller pipes for specific, global pipes for common tasks [OK]
Common Mistakes:
Binding all pipes globally causing unnecessary processing
Binding all pipes only on parameters causing repetition
Mixing global and method-level without clear reason
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.