@Param('id', ParseIntPipe). What will happen if the URL parameter id is 'abc'?import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get(':id') getItem(@Param('id', ParseIntPipe) id: number) { return { id }; } }
The ParseIntPipe automatically converts the input to a number. If the input is not a valid number string, it throws a BadRequestException, resulting in a 400 error response.
@Query('active', ParseBoolPipe) in a NestJS controller, what will be the value of active if the query parameter is ?active=false?import { Controller, Get, Query, ParseBoolPipe } from '@nestjs/common'; @Controller('users') export class UsersController { @Get() getUsers(@Query('active', ParseBoolPipe) active: boolean) { return { active }; } }
The ParseBoolPipe converts the string 'false' to the boolean false. It only accepts 'true' or 'false' (case insensitive) and throws an error otherwise.
ParseIntPipe to convert the userId route parameter to a number.The correct syntax is @Param('paramName', Pipe). Option A correctly applies ParseIntPipe to the userId parameter.
getFlag(@Query('flag', ParseBoolPipe) flag: boolean) { return flag; }
What causes the error when the query string is ?flag=yes?ParseBoolPipe only accepts the strings 'true' or 'false' (case insensitive). Any other string, like 'yes', causes it to throw a BadRequestException.
ParseIntPipe and ParseBoolPipe in NestJS controllers helps API reliability.These pipes validate and convert inputs before the controller logic runs. This stops invalid data early and sends clear error responses, making APIs more robust and easier to maintain.