Bird
Raised Fist0
NestJSframework~20 mins

Default value pipe in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Default Value Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when using DefaultValuePipe with a missing query parameter?

Consider this NestJS controller method:

  @Get('items')
  getItems(@Query('limit', new DefaultValuePipe('10')) limit: string) {
    return limit;
  }

If a client calls /items without the limit query parameter, what will be the response?

A"10"
Bundefined
Cnull
D0
Attempts:
2 left
💡 Hint

Think about what DefaultValuePipe does when the parameter is missing.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies DefaultValuePipe to a route parameter?

Given this NestJS route handler, which option correctly applies DefaultValuePipe to the id route parameter with a default of '42'?

  @Get(':id')
  getById( /* what goes here? */ ) {
    return id;
  }
A@Param('id', new DefaultValuePipe(42)) id: number
B@Param('id', new DefaultValuePipe('42')) id: string
C@Param('id', DefaultValuePipe('42')) id: string
D@Param('id', new DefaultValuePipe) id: string = '42'
Attempts:
2 left
💡 Hint

Remember how to instantiate pipes and pass default values.

🔧 Debug
advanced
2:00remaining
Why does this DefaultValuePipe usage cause a runtime error?

Examine this code snippet:

  @Get()
  getData(@Query('page', new DefaultValuePipe(1)) page: number) {
    return page + 1;
  }

When calling /endpoint without the page query parameter, a runtime error occurs. Why?

AThe query parameter 'page' is required and cannot have a default.
BDefaultValuePipe cannot accept numbers as default values, only strings.
CThe pipe is not instantiated correctly; it needs parentheses after the class name.
DDefaultValuePipe returns a string '1', but page is typed as number, causing a type error when adding.
Attempts:
2 left
💡 Hint

Consider the type of the default value returned by DefaultValuePipe.

🧠 Conceptual
advanced
2:00remaining
What happens if DefaultValuePipe is combined with ParseIntPipe on a query parameter?

Consider this NestJS method:

  @Get()
  getItems(
    @Query('limit', new DefaultValuePipe('5'), new ParseIntPipe()) limit: number
  ) {
    return limit * 2;
  }

If the client calls /items without the limit parameter, what will be the output?

A10
BNaN
C5
DError thrown by ParseIntPipe
Attempts:
2 left
💡 Hint

Think about the order pipes run and how ParseIntPipe converts strings.

❓ state_output
expert
2:00remaining
What is the value of 'offset' after this NestJS controller method runs?

Given this method:

  @Get()
  getOffset(
    @Query('offset', new DefaultValuePipe('0'), new ParseIntPipe()) offset: number
  ) {
    return offset;
  }

If the client calls /endpoint?offset=abc, what happens?

Aoffset is 0 because DefaultValuePipe sets default and ParseIntPipe converts it
Boffset is NaN because 'abc' cannot be parsed to number
CParseIntPipe throws a BadRequestException due to invalid number
Doffset is the string 'abc' because DefaultValuePipe overrides
Attempts:
2 left
💡 Hint

What does ParseIntPipe do when it cannot parse a string to a number?

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

  1. 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.
  2. Step 2: Compare with other pipe functions

    Unlike validation or transformation pipes, DefaultValuePipe does not validate or convert but only sets a fallback value.
  3. Final Answer:

    To provide a fallback value when a request parameter is missing -> Option A
  4. 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

  1. 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.
  2. Step 2: Confirm the correct usage of DefaultValuePipe

    The DefaultValuePipe requires a value in its constructor, so new DefaultValuePipe(10) is correct.
  3. Final Answer:

    @Query('page', new DefaultValuePipe(10)) page: number -> Option A
  4. 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

  1. Step 1: Analyze the request URL and query parameters

    The URL /items has no limit query parameter provided.
  2. Step 2: Understand DefaultValuePipe behavior

    Since limit is missing, DefaultValuePipe sets it to the default value 5.
  3. Final Answer:

    5 -> Option C
  4. 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:
@Get('users')
getUsers(@Query('page', DefaultValuePipe(1)) page: number) {
  return page;
}
medium
A. Default value must be a string, not a number
B. DefaultValuePipe cannot be used with @Query decorator
C. Parameter 'page' should be optional with '?'
D. Missing 'new' keyword when instantiating DefaultValuePipe

Solution

  1. Step 1: Check how DefaultValuePipe is instantiated

    The pipe must be created with the new keyword, but here it is called like a function.
  2. Step 2: Confirm correct usage with @Query

    DefaultValuePipe works with @Query, so no error there.
  3. Final Answer:

    Missing 'new' keyword when instantiating DefaultValuePipe -> Option D
  4. 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

  1. Step 1: Understand DefaultValuePipe limitations

    DefaultValuePipe only sets default if value is missing (undefined), not if empty string is provided.
  2. 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'.
  3. 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.
  4. Final Answer:

    Use a custom pipe that returns 'asc' if value is missing or empty string -> Option B
  5. Quick Check:

    DefaultValuePipe ignores empty string, custom pipe needed [OK]
Hint: DefaultValuePipe skips empty strings; use custom pipe [OK]
Common Mistakes:
  • Assuming DefaultValuePipe handles empty strings
  • Setting default in parameter but ignoring pipe
  • Not handling empty string case explicitly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard