0
0
NestJSframework~10 mins

Built-in pipes (ParseIntPipe, ParseBoolPipe) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in pipes (ParseIntPipe, ParseBoolPipe)
Receive Request Parameter as String
Apply Pipe: ParseIntPipe or ParseBoolPipe
Valid Conversion
Pass Converted Value
Controller Method Executes
The pipe takes a string input from a request, tries to convert it to int or bool, passes it if valid, or throws an error if invalid.
Execution Sample
NestJS
@Get(':id')
getById(@Param('id', ParseIntPipe) id: number) {
  return `ID is ${id}`;
}
This code converts the 'id' route parameter from string to number using ParseIntPipe before using it.
Execution Table
StepInput ParameterPipe AppliedConversion ResultAction TakenOutput or Error
1'123'ParseIntPipe123 (number)Pass converted value`ID is 123`
2'abc'ParseIntPipeNaN (invalid)Throw BadRequestException400 Bad Request Error
3'true'ParseBoolPipetrue (boolean)Pass converted valuetrue
4'false'ParseBoolPipefalse (boolean)Pass converted valuefalse
5'yes'ParseBoolPipeInvalid booleanThrow BadRequestException400 Bad Request Error
💡 Execution stops when conversion fails and pipe throws BadRequestException.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
inputParamundefined'123''abc''true''false''yes'
convertedValueundefined123NaNtruefalseInvalid
actionnonepassthrow errorpasspassthrow error
Key Moments - 2 Insights
Why does ParseIntPipe throw an error when the input is 'abc'?
Because 'abc' cannot be converted to a number, the pipe detects NaN and throws a BadRequestException as shown in execution_table step 2.
What happens if ParseBoolPipe receives 'yes' as input?
ParseBoolPipe only accepts 'true' or 'false' strings (case insensitive). 'yes' is invalid, so it throws a BadRequestException as in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when input parameter is '123' with ParseIntPipe?
A`ID is 123`
B400 Bad Request Error
CNaN
Dundefined
💡 Hint
Check execution_table row 1 under Output or Error column.
At which step does the condition cause a BadRequestException due to invalid boolean?
AStep 3
BStep 2
CStep 5
DStep 4
💡 Hint
Look at execution_table rows with ParseBoolPipe and check Conversion Result and Action Taken.
If the input parameter changes from 'false' to 'true' with ParseBoolPipe, what changes in variable_tracker?
AinputParam changes from true to false
BconvertedValue changes from false to true
Caction changes from throw error to pass
DNo change in variables
💡 Hint
Check variable_tracker rows for convertedValue at After Step 3 and After Step 4.
Concept Snapshot
Built-in pipes like ParseIntPipe and ParseBoolPipe convert string inputs from requests into numbers or booleans.
If conversion succeeds, the converted value is passed to the controller.
If conversion fails, a BadRequestException is thrown, stopping execution.
Use pipes in @Param or @Query decorators to validate and transform inputs automatically.
Full Transcript
In NestJS, built-in pipes such as ParseIntPipe and ParseBoolPipe help convert string inputs from HTTP requests into numbers or booleans. When a request comes in, the pipe tries to convert the string parameter. If successful, it passes the converted value to the controller method. If the conversion fails, the pipe throws a BadRequestException, which results in a 400 error response. For example, ParseIntPipe converts '123' to number 123, but throws an error for 'abc'. ParseBoolPipe converts 'true' or 'false' strings to booleans, but throws an error for invalid strings like 'yes'. Using these pipes ensures your controller receives correctly typed data and handles invalid inputs gracefully.