Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to bind a pipe to a single parameter in a NestJS controller method.
NestJS
async getUser(@Param('id', [1]) id: number) { return this.userService.findById(id); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting 'id' (the parameter name) instead of a pipe like ParseIntPipe
Confusing pipes with other decorators like @Body or @Query
✗ Incorrect
To bind a pipe to a single parameter, provide the pipe class as the second argument to the @Param decorator, such as ParseIntPipe to convert the string ID to a number.
2fill in blank
mediumComplete the code to apply a pipe to an entire controller method.
NestJS
@UsePipes([1]) async create(@Body() data: CreateDto) { return this.service.create(data); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class that is not a pipe inside @UsePipes
Confusing @UsePipes with @UseGuards or other decorators
✗ Incorrect
UsePipes decorator applies the specified pipe(s) to the whole method; ValidationPipe is commonly used for DTO validation.
3fill in blank
hardFix the error in binding a pipe globally in the main.ts file.
NestJS
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new [1]());
await app.listen(3000);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-pipe classes like Controller or HttpException
Forgetting to instantiate the pipe with 'new'
✗ Incorrect
ValidationPipe is the correct pipe to use globally for validating all incoming requests.
4fill in blank
hardFill both blanks to bind a pipe to a controller and a method.
NestJS
@UsePipes([1]) @Controller('items') export class ItemsController { @UsePipes([2]) async update(@Param('id', ParseIntPipe) id: number) { return this.itemsService.update(id); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-pipe classes in @UsePipes
Mixing up controller and method pipe usage
✗ Incorrect
ValidationPipe is applied at the controller level for general validation; ParseIntPipe is applied at the method level to parse the 'id' parameter.
5fill in blank
hardFill all three blanks to create a custom pipe and bind it globally.
NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class [1] implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { if (!value) { throw new [2]('Value is required'); } return value.trim(); } } async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new [3]()); await app.listen(3000); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception class
Not instantiating the pipe with 'new' when binding globally
Mismatching class names
✗ Incorrect
TrimPipe is the custom pipe class name; BadRequestException is thrown on invalid input; the global pipe instance is created with new TrimPipe().