Pipes help convert and validate data in NestJS. ParseIntPipe and ParseBoolPipe make sure your data is the right type, like numbers or true/false.
Built-in pipes (ParseIntPipe, ParseBoolPipe) in NestJS
import { ParseIntPipe, ParseBoolPipe } from '@nestjs/common'; @Get(':id') getById(@Param('id', ParseIntPipe) id: number) { // id is a number here } @Get('active/:status') getByStatus(@Param('status', ParseBoolPipe) status: boolean) { // status is a boolean here }
Use pipes inside parameter decorators like @Param(), @Query(), or @Body() to transform input data.
If the input cannot be converted, NestJS will automatically return a 400 error.
@Get(':id') getUser(@Param('id', ParseIntPipe) id: number) { return `User ID is ${id}`; }
@Get('active/:status') getActive(@Param('status', ParseBoolPipe) status: boolean) { return `Active status is ${status}`; }
@Get('query') getQuery(@Query('page', ParseIntPipe) page: number) { return `Page number is ${page}`; }
This controller has two routes. One converts the 'id' param to a number. The other converts 'status' param to a boolean. If conversion fails, NestJS sends a 400 error automatically.
import { Controller, Get, Param, ParseIntPipe, ParseBoolPipe } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get(':id') getItemById(@Param('id', ParseIntPipe) id: number) { return `Item ID is ${id}`; } @Get('available/:status') getItemAvailability(@Param('status', ParseBoolPipe) status: boolean) { return `Item availability is ${status}`; } }
ParseIntPipe converts strings to integers using parseInt(), which truncates decimals (e.g., '3.14' becomes 3) and rejects non-numeric inputs with an error.
ParseBoolPipe accepts 'true', 'false', '1', '0' (case insensitive) as valid boolean strings.
You can customize error messages by extending these pipes if needed.
ParseIntPipe and ParseBoolPipe automatically convert string inputs to number and boolean types.
They help keep your code clean by avoiding manual parsing and validation.
Invalid inputs cause NestJS to return a 400 error, improving API reliability.
Practice
ParseIntPipe and ParseBoolPipe?Solution
Step 1: Understand the role of pipes in NestJS
Pipes transform and validate incoming data before it reaches the route handler.Step 2: Identify what ParseIntPipe and ParseBoolPipe do
They convert string inputs to integer and boolean types respectively, ensuring correct data types.Final Answer:
To automatically convert and validate input types like numbers and booleans -> Option DQuick Check:
Pipe purpose = convert and validate input [OK]
- Confusing pipes with output formatting
- Thinking pipes manage database or routing
- Assuming pipes only validate without conversion
ParseIntPipe in a NestJS controller method parameter?Solution
Step 1: Check the parameter decorator and pipe usage
Using @Param('id', ParseIntPipe) applies the pipe to convert the 'id' route parameter.Step 2: Match the parameter type with the pipe's output
ParseIntPipe converts input to a number, so the parameter type must benumber.Final Answer:
@Param('id', ParseIntPipe) id: number -> Option CQuick Check:
ParseIntPipe converts to number, so type = number [OK]
- Using string type with ParseIntPipe
- Applying pipe to wrong decorator like @Body for route param
- Assigning boolean type with ParseIntPipe
@Get(':active')
getStatus(@Param('active', ParseBoolPipe) active: boolean) {
return active ? 'Active' : 'Inactive';
}
What will be the response if the client requests /true?Solution
Step 1: Understand ParseBoolPipe behavior
ParseBoolPipe converts string 'true' to boolean true.Step 2: Evaluate the method return based on boolean value
Since active is true, the method returns 'Active'.Final Answer:
"Active" -> Option AQuick Check:
Input 'true' -> boolean true -> returns 'Active' [OK]
- Expecting string 'true' instead of boolean
- Assuming 400 error for valid boolean string
- Confusing output with input string
@Get(':id')
getUser(@Param('id', ParseIntPipe) id: number) {
return `User ID is ${id}`;
}
If a client sends a request to /abc, what will happen?Solution
Step 1: Understand ParseIntPipe input validation
ParseIntPipe tries to convert 'abc' to a number but fails.Step 2: Identify NestJS behavior on invalid pipe input
When conversion fails, NestJS returns a 400 Bad Request error automatically.Final Answer:
Throws a 400 Bad Request error -> Option AQuick Check:
Invalid int input -> 400 error [OK]
- Expecting string output instead of error
- Assuming NaN is returned
- Thinking it defaults to 0
enabled which should be a boolean. Which of the following is the best way to ensure enabled is automatically converted and validated as a boolean?Solution
Step 1: Identify the correct decorator for query parameters
Query parameters use the @Query decorator.Step 2: Match the pipe and parameter type
ParseBoolPipe converts string to boolean, so parameter type must be boolean.Final Answer:
@Query('enabled', ParseBoolPipe) enabled: boolean -> Option BQuick Check:
Query param + ParseBoolPipe + boolean type = correct [OK]
- Using ParseIntPipe for boolean values
- Using @Body or @Param instead of @Query for query params
- Mismatching parameter type with pipe output
Ready to prove it?
Pass the test and this pattern moves unknown
