0
0
NestJSframework~10 mins

Custom pipes in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AController
BPipeTransform
CModule
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module decorators instead of @Injectable.
Not implementing PipeTransform interface.
2fill in blank
medium

Complete 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'
ANotFoundException
BHttpException
CUnauthorizedException
DBadRequestException
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing exceptions unrelated to validation like NotFoundException.
Not throwing any exception on invalid input.
3fill in blank
hard

Fix 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'
Acontext: ExecutionContext
Bargs: any[]
Cmetadata: ArgumentMetadata
Drequest: Request
Attempts:
3 left
💡 Hint
Common Mistakes
Using ExecutionContext which is for guards and interceptors.
Omitting the second parameter or using wrong types.
4fill in blank
hard

Fill 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'
Aid
BParseIntPipe
Cbody
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name like 'body'.
Applying a different pipe than the custom one.
5fill in blank
hard

Fill 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'
Avalue
B<=
CBadRequestException
Dvalue.toString()
Attempts:
3 left
💡 Hint
Common Mistakes
Parsing the raw value without converting to string.
Using wrong comparison operator.
Throwing generic Error instead of BadRequestException.