What is the purpose of the ParseIntPipe in NestJS?
ParseIntPipe converts a string input from a request into an integer. It ensures the value is a valid number and throws an error if not.
Click to reveal answer
beginner
How does ParseBoolPipe interpret string values?
ParseBoolPipe converts string inputs like 'true' or 'false' (case insensitive) into boolean true or false. It throws an error for invalid boolean strings.
Click to reveal answer
beginner
Show a simple example of using ParseIntPipe in a controller route parameter.
In a controller method, use @Param('id', ParseIntPipe) id: number to automatically convert the 'id' parameter to a number.
Click to reveal answer
intermediate
What happens if ParseIntPipe receives a non-numeric string?
It throws a BadRequestException, causing NestJS to respond with a 400 error indicating invalid input.
Click to reveal answer
intermediate
Why use built-in pipes like ParseIntPipe and ParseBoolPipe instead of manual conversion?
They simplify code by automatically validating and converting inputs, improving reliability and reducing boilerplate error checks.
Click to reveal answer
What does ParseIntPipe do in NestJS?
AConverts string input to an integer
BConverts boolean input to string
CParses JSON objects
DEncrypts input data
✗ Incorrect
ParseIntPipe converts string inputs to integers and validates them.
Which string value will ParseBoolPipe convert to false?
A"true"
B"false"
C"yes"
D"1"
✗ Incorrect
ParseBoolPipe converts 'false' (case insensitive) to boolean false.
What error does ParseIntPipe throw on invalid input?
AInternalServerErrorException
BUnauthorizedException
CNotFoundException
DBadRequestException
✗ Incorrect
ParseIntPipe throws BadRequestException for invalid integer input.
How do you apply ParseIntPipe to a route parameter named 'id'?
A@Param('id', ParseIntPipe) id: number
B@Body('id', ParseIntPipe) id: number
C@Query('id', ParseIntPipe) id: number
D@Headers('id', ParseIntPipe) id: number
✗ Incorrect
Use @Param decorator with ParseIntPipe to convert route parameters.
Why prefer built-in pipes over manual parsing?
AThey add extra features like caching
BThey slow down the app for security
CThey automatically validate and convert inputs
DThey replace database queries
✗ Incorrect
Built-in pipes simplify input validation and conversion, reducing errors.
Explain how ParseIntPipe works and when you would use it in a NestJS controller.
Think about converting URL parameters to numbers safely.
You got /4 concepts.
Describe the behavior of ParseBoolPipe and how it handles different string inputs.
Consider how to safely get true/false from user input.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of NestJS built-in pipes like ParseIntPipe and ParseBoolPipe?
easy
A. To create new HTTP routes in the application
B. To format output data before sending it to the client
C. To handle database connections automatically
D. To automatically convert and validate input types like numbers and booleans
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 D
Quick Check:
Pipe purpose = convert and validate input [OK]
Hint: Pipes transform inputs before controller methods run [OK]
Common Mistakes:
Confusing pipes with output formatting
Thinking pipes manage database or routing
Assuming pipes only validate without conversion
2. Which of the following is the correct way to use ParseIntPipe in a NestJS controller method parameter?
easy
A. @Body('id', ParseIntPipe) id: string
B. @Param('id', ParseIntPipe) id: string
C. @Param('id', ParseIntPipe) id: number
D. @Query('id', ParseIntPipe) id: boolean
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 be number.
Final Answer:
@Param('id', ParseIntPipe) id: number -> Option C
Quick Check:
ParseIntPipe converts to number, so type = number [OK]
Hint: ParseIntPipe means parameter type must be number [OK]
Common Mistakes:
Using string type with ParseIntPipe
Applying pipe to wrong decorator like @Body for route param
Hint: ParseBoolPipe converts 'true' string to boolean true [OK]
Common Mistakes:
Expecting string 'true' instead of boolean
Assuming 400 error for valid boolean string
Confusing output with input string
4. Consider this NestJS controller method:
@Get(':id')
getUser(@Param('id', ParseIntPipe) id: number) {
return `User ID is ${id}`;
}
If a client sends a request to /abc, what will happen?
medium
A. Throws a 400 Bad Request error
B. Returns 'User ID is NaN'
C. Returns 'User ID is abc'
D. Returns 'User ID is 0'
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 A
Quick Check:
Invalid int input -> 400 error [OK]
Hint: Invalid number input with ParseIntPipe causes 400 error [OK]
Common Mistakes:
Expecting string output instead of error
Assuming NaN is returned
Thinking it defaults to 0
5. You want to create a NestJS route that accepts a query parameter 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?
hard
A. @Query('enabled', ParseIntPipe) enabled: boolean
B. @Query('enabled', ParseBoolPipe) enabled: boolean
C. @Body('enabled', ParseBoolPipe) enabled: string
D. @Param('enabled', ParseBoolPipe) enabled: 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 B
Quick Check:
Query param + ParseBoolPipe + boolean type = correct [OK]
Hint: Use @Query with ParseBoolPipe for boolean query params [OK]
Common Mistakes:
Using ParseIntPipe for boolean values
Using @Body or @Param instead of @Query for query params
Mismatching parameter type with pipe output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.