0
0
NestJSframework~20 mins

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

Choose your learning style9 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.