Bird
Raised Fist0
NestJSframework~20 mins

Pipe binding (parameter, method, controller, global) in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pipe Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when a pipe is bound at the parameter level?

Consider a NestJS controller method where a custom pipe is applied only to a single parameter. What will be the effect of this pipe binding?

NestJS
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get(':id')
  getItem(@Param('id', ParseIntPipe) id: number) {
    return { id, type: typeof id };
  }
}
AThe pipe is ignored because it must be applied at the controller level to work.
BThe pipe converts the 'id' parameter to a number only for this method call, so the returned type is 'number'.
CThe pipe converts all parameters named 'id' in all controllers to number globally.
DThe pipe converts the 'id' parameter to a string instead of a number.
Attempts:
2 left
💡 Hint

Think about where the pipe is applied and what it affects.

❓ component_behavior
intermediate
2:00remaining
What happens when a pipe is bound at the controller level?

Given a NestJS controller with a pipe applied at the controller level, how does this pipe affect incoming requests?

NestJS
import { Controller, Get, Param, UsePipes, ParseIntPipe } from '@nestjs/common';

@UsePipes(ParseIntPipe)
@Controller('users')
export class UsersController {
  @Get(':id')
  getUser(@Param('id') id: number) {
    return { id, type: typeof id };
  }

  @Get('profile/:id')
  getProfile(@Param('id') id: number) {
    return { profileId: id, type: typeof id };
  }
}
AThe ParseIntPipe is applied to all parameters in all methods of this controller, converting them to numbers.
BThe ParseIntPipe is applied only to the first method in the controller.
CThe ParseIntPipe is ignored because it must be applied globally to work.
DThe ParseIntPipe converts all string parameters to uppercase strings.
Attempts:
2 left
💡 Hint

Consider the scope of the @UsePipes decorator when applied to a controller.

❓ component_behavior
advanced
2:00remaining
What is the effect of binding a pipe globally in NestJS?

In a NestJS application, a pipe is bound globally in the main.ts file. How does this affect the request handling?

NestJS
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
}
bootstrap();
AThe ValidationPipe runs only on parameters decorated with @UsePipes(ValidationPipe).
BThe ValidationPipe runs only on controllers that explicitly use @UsePipes(ValidationPipe).
CThe ValidationPipe runs only on methods decorated with @UsePipes(ValidationPipe).
DThe ValidationPipe runs on every incoming request across all controllers and routes, validating inputs globally.
Attempts:
2 left
💡 Hint

Think about what 'global' means in this context.

📝 Syntax
advanced
2:00remaining
Which option correctly applies a custom pipe only to a specific method in a NestJS controller?

Choose the correct way to apply a custom pipe named MyCustomPipe only to the create method of a controller.

NestJS
import { Controller, Post, Body, UsePipes } from '@nestjs/common';
import { MyCustomPipe } from './my-custom.pipe';

@Controller('products')
export class ProductsController {
  @Post()
  create(@Body() data: any) {
    return data;
  }
}
A
@Post()
create(@Body(MyCustomPipe) data: any) {
  return data;
}
B
@UsePipes(MyCustomPipe)
@Controller('products')
@Post()
create(@Body() data: any) {
  return data;
}
C
@Post()
@UsePipes(MyCustomPipe)
create(@Body() data: any) {
  return data;
}
D
@Controller('products')
@UsePipes(MyCustomPipe)
@Post()
create(@Body() data: any) {
  return data;
}
Attempts:
2 left
💡 Hint

Remember where to place the @UsePipes decorator to affect only one method.

🔧 Debug
expert
3:00remaining
Why does this global pipe not transform input as expected?

Given the following NestJS setup, why does the global pipe not transform the incoming string to a number?

NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common';

@Injectable()
export class ToNumberPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    return Number(value);
  }
}

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ToNumberPipe());
  await app.listen(3000);
}
bootstrap();

// Controller method
import { Controller, Get, Param } from '@nestjs/common';

@Controller('orders')
export class OrdersController {
  @Get(':id')
  getOrder(@Param('id') id: number) {
    return { id, type: typeof id };
  }
}
AThe global pipe runs but does not transform because the parameter decorator @Param does not trigger transformation by default.
BThe pipe throws a runtime error because Number(value) is invalid for string inputs.
CThe pipe is never called because it is not bound at the controller or method level.
DThe pipe transforms the value correctly, so the type of id is 'number'.
Attempts:
2 left
💡 Hint

Consider how NestJS handles parameter transformation and when pipes run.

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