Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a custom pipe class in NestJS.
NestJS
import { PipeTransform, Injectable } from '@nestjs/common'; @Injectable() export class ParseIntPipe implements [1] { transform(value: any) { return parseInt(value, 10); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module decorators instead of @Injectable.
Not implementing PipeTransform interface.
✗ Incorrect
The custom pipe class must implement the PipeTransform interface to define the transform method.
2fill in blank
mediumComplete the code to throw an exception if the value is not a number in the custom pipe.
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; @Injectable() export class ParseIntPipe implements PipeTransform { transform(value: any) { const val = parseInt(value, 10); if (isNaN(val)) { throw new [1]('Validation failed'); } return val; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing exceptions unrelated to validation like NotFoundException.
Not throwing any exception on invalid input.
✗ Incorrect
BadRequestException is used to indicate that the client sent invalid data.
3fill in blank
hardFix the error in the custom pipe to correctly implement the transform method signature.
NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class ParseIntPipe implements PipeTransform { transform(value: any, [1]) { const val = parseInt(value, 10); if (isNaN(val)) { throw new Error('Validation failed'); } return val; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ExecutionContext which is for guards and interceptors.
Omitting the second parameter or using wrong types.
✗ Incorrect
The transform method receives the value and an ArgumentMetadata object describing the parameter.
4fill in blank
hardFill both blanks to apply the custom pipe to a controller route parameter.
NestJS
import { Controller, Get, Param } from '@nestjs/common'; import { ParseIntPipe } from './parse-int.pipe'; @Controller('items') export class ItemsController { @Get(':id') findOne(@Param('[1]', [2]) id: number) { return `Item #${id}`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name like 'body'.
Applying a different pipe than the custom one.
✗ Incorrect
The route parameter name is 'id' and the custom pipe ParseIntPipe is applied to transform it.
5fill in blank
hardFill all three blanks to create a reusable custom pipe that validates a positive integer.
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; @Injectable() export class PositiveIntPipe implements PipeTransform { transform(value: any) { const val = parseInt([1], 10); if (isNaN(val) || val [2] 0) { throw new [3]('Value must be a positive integer'); } return val; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Parsing the raw value without converting to string.
Using wrong comparison operator.
Throwing generic Error instead of BadRequestException.
✗ Incorrect
Convert value to string before parsing, check if val is less or equal to zero, and throw BadRequestException on failure.