A pipe in NestJS is a class that transforms or validates data before it reaches the route handler. It helps ensure data is correct and formatted properly.
Click to reveal answer
beginner
How do you bind a pipe to a single parameter in NestJS?
You bind a pipe to a parameter by adding it inside the decorator, like @Param('id', ParseIntPipe). This means only that parameter will be processed by the pipe.
Click to reveal answer
intermediate
What happens when you bind a pipe at the method level in NestJS?
Binding a pipe at the method level applies it to all parameters of that method. The pipe runs before the method executes, transforming or validating all inputs.
Click to reveal answer
intermediate
How do you apply a pipe to all routes in a controller?
You add the pipe in the @UsePipes() decorator on the controller class. This makes the pipe run for every route handler inside that controller.
Click to reveal answer
intermediate
What is the effect of binding a pipe globally in NestJS?
Binding a pipe globally means it runs on every request in the app. You do this by adding the pipe in the main.ts file with app.useGlobalPipes().
Click to reveal answer
Where do you bind a pipe to affect only one parameter in NestJS?
AIn main.ts with app.useGlobalPipes()
BOn the controller class with @UsePipes()
CInside the parameter decorator, like @Param('id', Pipe)
DOn the method with @UsePipes()
✗ Incorrect
Binding a pipe inside the parameter decorator applies it only to that parameter.
What decorator do you use to apply a pipe to all routes in a controller?
A@UsePipes() on the controller class
B@ControllerPipe()
C@GlobalPipe()
D@Pipe() on each method
✗ Incorrect
Use @UsePipes() on the controller class to apply pipes to all routes inside.
How do you apply a pipe globally in a NestJS app?
ABind it to each parameter decorator
BAdd it to each controller with @UsePipes()
CUse @GlobalPipe() decorator on the pipe class
DAdd it in main.ts with app.useGlobalPipes()
✗ Incorrect
Global pipes are set in main.ts using app.useGlobalPipes().
What is the effect of binding a pipe at the method level?
AIt applies the pipe only to one parameter
BIt applies the pipe to all parameters of that method
CIt applies the pipe globally to all controllers
DIt disables pipes for that method
✗ Incorrect
Binding a pipe at the method level applies it to all parameters of that method.
Which of these is NOT a valid way to bind a pipe in NestJS?
ABinding inside the service class constructor
BBinding on a method with @UsePipes()
CBinding on a controller with @UsePipes()
DBinding inside a parameter decorator
✗ Incorrect
Pipes are not bound inside service constructors; they are bound on parameters, methods, controllers, or globally.
Explain how to bind a pipe at different levels in NestJS and what each level affects.
Think about where you place the pipe: on a parameter, method, controller, or globally in main.ts.
You got /4 concepts.
Describe the benefits of using pipes in NestJS and why you might choose different binding levels.
Consider how often you want the pipe to run and on which data.
You got /5 concepts.
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.