📖 Scenario: You are building a simple NestJS controller to handle user queries. Sometimes users may not provide a query parameter, so you want to set a default value automatically.
🎯 Goal: Create a NestJS controller with a GET endpoint that uses the DefaultValuePipe to provide a default value for a query parameter called page.
📋 What You'll Learn
Create a controller class named AppController.
Add a GET route handler method named getItems.
Use the @Query decorator with DefaultValuePipe to set a default value of 1 for the page query parameter.
Return the page value as a number from the handler.
💡 Why This Matters
🌍 Real World
Setting default values for query parameters is common in APIs to handle missing inputs gracefully.
💼 Career
Understanding pipes like DefaultValuePipe is essential for building robust and user-friendly APIs in NestJS.
Progress0 / 4 steps
1
Create the controller class and import necessary decorators
Create a class called AppController and import Controller and Get from @nestjs/common.
NestJS
Hint
Use export class AppController and decorate it with @Controller().
2
Add the GET route handler method
Inside AppController, add a method named getItems decorated with @Get() that returns 0 for now.
NestJS
Hint
Define getItems() method and decorate it with @Get().
3
Import and use DefaultValuePipe for the query parameter
Import Query and DefaultValuePipe from @nestjs/common. Modify getItems to accept a page parameter from query using @Query('page', new DefaultValuePipe('1')).
NestJS
Hint
Use @Query('page', new DefaultValuePipe('1')) to set default page value.
4
Convert the page parameter to a number before returning
Modify the getItems method to convert the page parameter from string to number using Number(page) and return it.
NestJS
Hint
Use Number(page) to convert the string to a number before returning.
Practice
(1/5)
1. What is the main purpose of the DefaultValuePipe in NestJS?
easy
A. To provide a fallback value when a request parameter is missing
B. To validate the type of a request parameter
C. To transform a string into a number
D. To throw an error if a parameter is missing
Solution
Step 1: Understand the role of DefaultValuePipe
The DefaultValuePipe is used to supply a default value when the expected input is not provided in the request.
Step 2: Compare with other pipe functions
Unlike validation or transformation pipes, DefaultValuePipe does not validate or convert but only sets a fallback value.
Final Answer:
To provide a fallback value when a request parameter is missing -> Option A
Quick Check:
DefaultValuePipe = fallback value [OK]
Hint: DefaultValuePipe sets fallback for missing inputs [OK]
Common Mistakes:
Confusing DefaultValuePipe with validation pipes
Thinking it throws errors on missing params
Assuming it transforms data types
2. Which of the following is the correct way to use DefaultValuePipe to set a default value of 10 for a query parameter named page?
easy
A. @Query('page', new DefaultValuePipe(10)) page: number
B. @Query('page', DefaultValuePipe(10)) page: number
C. @Query('page', DefaultValuePipe) page: number = 10
D. @Query('page', new DefaultValuePipe) page: number = 10
Solution
Step 1: Check the syntax for using pipes in NestJS
Pipes are instantiated with the new keyword and passed as the second argument to decorators like @Query.
Step 2: Confirm the correct usage of DefaultValuePipe
The DefaultValuePipe requires a value in its constructor, so new DefaultValuePipe(10) is correct.
Final Answer:
@Query('page', new DefaultValuePipe(10)) page: number -> Option A
Quick Check:
Use new DefaultValuePipe(value) with decorator [OK]
Hint: Always use 'new' to instantiate DefaultValuePipe [OK]
Common Mistakes:
Omitting 'new' keyword when creating the pipe
Passing DefaultValuePipe without parentheses
Assigning default value directly in parameter without pipe
3. Given the following controller method, what will be the value of limit if the request URL is /items (no query parameters)?
@Get('items')
getItems(@Query('limit', new DefaultValuePipe(5)) limit: number) {
return limit;
}
medium
A. undefined
B. null
C. 5
D. 0
Solution
Step 1: Analyze the request URL and query parameters
The URL /items has no limit query parameter provided.
Step 2: Understand DefaultValuePipe behavior
Since limit is missing, DefaultValuePipe sets it to the default value 5.
Final Answer:
5 -> Option C
Quick Check:
Missing param uses default value 5 [OK]
Hint: Missing query uses DefaultValuePipe value [OK]
Common Mistakes:
Assuming undefined when param is missing
Confusing null with default value
Expecting zero instead of default
4. Identify the error in this NestJS controller method using DefaultValuePipe:
B. DefaultValuePipe cannot be used with @Query decorator
C. Parameter 'page' should be optional with '?'
D. Missing 'new' keyword when instantiating DefaultValuePipe
Solution
Step 1: Check how DefaultValuePipe is instantiated
The pipe must be created with the new keyword, but here it is called like a function.
Step 2: Confirm correct usage with @Query
DefaultValuePipe works with @Query, so no error there.
Final Answer:
Missing 'new' keyword when instantiating DefaultValuePipe -> Option D
Quick Check:
Always use 'new' with DefaultValuePipe [OK]
Hint: Remember 'new' before DefaultValuePipe instance [OK]
Common Mistakes:
Calling DefaultValuePipe as a function without 'new'
Thinking DefaultValuePipe only works with @Param
Making parameter optional instead of using pipe
5. You want to create a controller method that accepts an optional sort query parameter with a default value of 'asc'. Additionally, if the client sends an empty string for sort, it should also default to 'asc'. Which approach correctly achieves this behavior?
hard
A. Use @Query('sort', new DefaultValuePipe('asc')) sort: string
B. Use a custom pipe that returns 'asc' if value is missing or empty string
C. Use @Query('sort', new DefaultValuePipe('asc')) sort: string = 'asc'
D. Use @Query('sort') sort: string = 'asc' without any pipe
Solution
Step 1: Understand DefaultValuePipe limitations
DefaultValuePipe only sets default if value is missing (undefined), not if empty string is provided.
Step 2: Consider handling empty string case
To treat empty string as missing, a custom pipe is needed to check both missing and empty values and return 'asc'.
Step 3: Evaluate other options
Use @Query('sort', new DefaultValuePipe('asc')) sort: string = 'asc' sets default but does not handle empty string; Use @Query('sort', new DefaultValuePipe('asc')) sort: string does not handle empty string; Use @Query('sort') sort: string = 'asc' without any pipe ignores pipe usage.
Final Answer:
Use a custom pipe that returns 'asc' if value is missing or empty string -> Option B