Discover how a simple pipe can save you from endless default checks!
Why Default value pipe in NestJS? - Purpose & Use Cases
Imagine building a web API where users can send requests with optional fields. You have to check if each field is missing and then manually assign default values every time.
Manually checking and assigning defaults in every controller method is repetitive, error-prone, and clutters your code. It's easy to forget a default or introduce bugs.
The Default value pipe in NestJS automatically assigns default values to missing or undefined inputs before your controller logic runs, keeping your code clean and consistent.
if (!body.limit) { body.limit = 10; }
@Body('limit', new DefaultValuePipe(10)) limit: number
This lets you write simpler, cleaner controllers that always receive valid inputs with sensible defaults.
When building a paginated API, you can ensure the page size defaults to 10 if the client doesn't specify it, without extra code in every handler.
Manually setting defaults is repetitive and error-prone.
Default value pipe automates default assignment cleanly.
It improves code readability and reduces bugs.
Practice
DefaultValuePipe in NestJS?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 AQuick Check:
DefaultValuePipe = fallback value [OK]
- Confusing DefaultValuePipe with validation pipes
- Thinking it throws errors on missing params
- Assuming it transforms data types
DefaultValuePipe to set a default value of 10 for a query parameter named page?Solution
Step 1: Check the syntax for using pipes in NestJS
Pipes are instantiated with thenewkeyword 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, sonew DefaultValuePipe(10)is correct.Final Answer:
@Query('page', new DefaultValuePipe(10)) page: number -> Option AQuick Check:
Usenew DefaultValuePipe(value)with decorator [OK]
- Omitting 'new' keyword when creating the pipe
- Passing DefaultValuePipe without parentheses
- Assigning default value directly in parameter without pipe
limit if the request URL is /items (no query parameters)?
@Get('items')
getItems(@Query('limit', new DefaultValuePipe(5)) limit: number) {
return limit;
}Solution
Step 1: Analyze the request URL and query parameters
The URL/itemshas nolimitquery parameter provided.Step 2: Understand DefaultValuePipe behavior
Sincelimitis missing, DefaultValuePipe sets it to the default value 5.Final Answer:
5 -> Option CQuick Check:
Missing param uses default value 5 [OK]
- Assuming undefined when param is missing
- Confusing null with default value
- Expecting zero instead of default
DefaultValuePipe:
@Get('users')
getUsers(@Query('page', DefaultValuePipe(1)) page: number) {
return page;
}Solution
Step 1: Check how DefaultValuePipe is instantiated
The pipe must be created with thenewkeyword, 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 DQuick Check:
Always use 'new' with DefaultValuePipe [OK]
- Calling DefaultValuePipe as a function without 'new'
- Thinking DefaultValuePipe only works with @Param
- Making parameter optional instead of using pipe
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?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: stringdoes 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 BQuick Check:
DefaultValuePipe ignores empty string, custom pipe needed [OK]
- Assuming DefaultValuePipe handles empty strings
- Setting default in parameter but ignoring pipe
- Not handling empty string case explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
