Bird
Raised Fist0
NestJSframework~5 mins

Pipe binding (parameter, method, controller, global) in NestJS - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a pipe in NestJS?
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()
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
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()
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
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
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

      1. 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.
      2. Step 2: Differentiate from other bindings

        Middleware and global pipes affect broader scopes, not just a single parameter. Routing is unrelated to pipes.
      3. Final Answer:

        To transform or validate the specific parameter before the method uses it -> Option D
      4. Quick Check:

        Parameter pipe = transform/validate parameter [OK]
      Hint: Parameter pipes affect only one input value [OK]
      Common Mistakes:
      • Confusing parameter pipes with global pipes
      • Thinking pipes define routes
      • Mixing middleware with pipes
      2. Which of the following is the correct syntax to bind a pipe globally in a NestJS application?
      easy
      A. @UsePipes(new ValidationPipe()) on a controller class
      B. app.useGlobalPipes(new ValidationPipe());
      C. @UsePipes(new ValidationPipe()) on a method
      D. Bind pipe inside a method parameter decorator

      Solution

      1. Step 1: Identify global pipe binding syntax

        Global pipes are bound using the app instance with app.useGlobalPipes(pipeInstance).
      2. Step 2: Differentiate from controller and method bindings

        @UsePipes decorator applies pipes at controller or method level, not globally.
      3. Final Answer:

        app.useGlobalPipes(new ValidationPipe()); -> Option B
      4. Quick Check:

        Global pipe = app.useGlobalPipes() [OK]
      Hint: Global pipes use app.useGlobalPipes() method [OK]
      Common Mistakes:
      • Using @UsePipes for global binding
      • Binding pipes inside method parameters for global effect
      • Confusing middleware with pipes
      3. Given the following code, what will be the output if the incoming request has a query parameter age='25' (string)?
      import { Controller, Get, Query, ParseIntPipe } from '@nestjs/common';
      
      @Controller('users')
      export class UserController {
        @Get()
        getUser(@Query('age', ParseIntPipe) age: number) {
          return { age, type: typeof age };
        }
      }
      medium
      A. Runtime error due to type mismatch
      B. {"age": "25", "type": "string"}
      C. {"age": 25, "type": "number"}
      D. {"age": null, "type": "object"}

      Solution

      1. Step 1: Understand ParseIntPipe effect on query parameter

        ParseIntPipe converts the string '25' to the number 25 before passing it to the method.
      2. Step 2: Check returned object values

        The method returns an object with age as number 25 and type as 'number'.
      3. Final Answer:

        {"age": 25, "type": "number"} -> Option C
      4. Quick Check:

        ParseIntPipe converts string to number [OK]
      Hint: ParseIntPipe converts string query to number [OK]
      Common Mistakes:
      • Assuming age remains string
      • Expecting runtime error on conversion
      • Confusing type of returned value
      4. Identify the error in this NestJS controller code snippet where a pipe is bound at the method level:
      import { Controller, Get, UsePipes, ValidationPipe, Query } from '@nestjs/common';
      
      @Controller('products')
      export class ProductController {
        @Get()
        @UsePipes(ValidationPipe)
        findProduct(@Query('id') id: string) {
          return id;
        }
      }
      medium
      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

      1. Step 1: Check @UsePipes decorator usage

        @UsePipes expects an instance of a pipe, so ValidationPipe must be called with 'new'.
      2. Step 2: Validate other parts

        @UsePipes can be used on methods, Query decorator is valid, Controller decorator is correct.
      3. Final Answer:

        ValidationPipe should be instantiated with 'new' keyword in @UsePipes -> Option A
      4. 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

      1. 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.
      2. Step 2: Apply number validation globally

        Number validation is common across the app, so global binding ensures consistent validation everywhere.
      3. Final Answer:

        Bind the trim pipe at controller level and the number validation pipe globally -> Option A
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard