0
0
NestJSframework~10 mins

Default value pipe in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default value pipe
Request with param
Pipe intercepts param
Check if param is undefined or null
Yes No
Replace with default
Pass value to handler
Handler uses value
The Default value pipe checks if a parameter is missing and replaces it with a default before the handler uses it.
Execution Sample
NestJS
import { PipeTransform, Injectable } from '@nestjs/common';

@Injectable()
export class DefaultValuePipe implements PipeTransform {
  constructor(private readonly defaultValue: any) {}
  transform(value: any) {
    return value == null ? this.defaultValue : value;
  }
}
This pipe replaces null or undefined values with a default value before passing to the controller.
Execution Table
StepInput ValueCheck value == nullActionOutput Value
1undefinedtrueReplace with default 'guest'guest
2nulltrueReplace with default 'guest'guest
3'admin'falseKeep originaladmin
4'' (empty string)falseKeep original
50falseKeep original0
6falsefalseKeep originalfalse
7nulltrueReplace with default 'guest'guest
💡 All inputs processed; pipe replaced only null or undefined values with default.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7
valueundefinedundefinednull'admin'''0falsenull
outputN/Aguestguestadmin''0falseguest
Key Moments - 2 Insights
Why does the pipe replace undefined and null but not empty string or 0?
Because the pipe checks specifically for value == null, which matches only null or undefined, not other falsy values like empty string or zero (see execution_table rows 1, 2 vs 4, 5).
What happens if the input is false? Does the pipe replace it?
No, false is a valid value and not null or undefined, so the pipe keeps it as is (see execution_table row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what output does the pipe produce when input is null at step 2?
Aundefined
Bnull
Cguest
Derror
💡 Hint
Check the 'Output Value' column at step 2 in the execution_table.
At which step does the pipe keep the original input 'admin' without replacing it?
AStep 1
BStep 3
CStep 5
DStep 7
💡 Hint
Look for the input value 'admin' in the execution_table and see the action taken.
If the default value was changed to 'user', what would be the output at step 7?
Auser
Bnull
Cguest
Dundefined
💡 Hint
Step 7 input is null, so the pipe replaces it with the default value (see variable_tracker and execution_table).
Concept Snapshot
Default Value Pipe in NestJS:
- Checks if input is null or undefined
- If yes, replaces with a default value
- Otherwise, keeps original input
- Used to ensure parameters have fallback values
- Implement by creating a class with transform method
- Helps avoid undefined errors in handlers
Full Transcript
The Default Value Pipe in NestJS intercepts incoming parameter values. It checks if the value is null or undefined. If so, it replaces the value with a preset default. Otherwise, it passes the original value unchanged. This ensures that controller handlers always receive a valid value, avoiding errors from missing parameters. The pipe is implemented as a class with a transform method that performs this check and replacement. The execution table shows how different inputs are handled step-by-step, with null and undefined replaced by the default, and other values kept as is.