Complete the code to create a custom pipe class in NestJS.
import { PipeTransform, Injectable } from '@nestjs/common'; @Injectable() export class ParseIntPipe implements [1] { transform(value: any) { return parseInt(value, 10); } }
Complete the code to throw an exception if the value is not a number in the custom pipe.
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; } }
Fix the error in the custom pipe to correctly implement the transform method signature.
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; } }
Fill both blanks to apply the custom pipe to a controller route parameter.
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}`; } }
Fill all three blanks to create a reusable custom pipe that validates a positive integer.
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; } }
