0
0
NestJSframework~10 mins

Custom pipes in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom pipes
Request received
Pipe invoked
Transform or Validate input
Valid
Pass to
Controller
When a request comes in, NestJS runs the custom pipe to check or change data before the controller uses it.
Execution Sample
NestJS
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class ParseIntPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new BadRequestException('Validation failed');
    }
    return val;
  }
}
This pipe tries to convert input to an integer and throws an error if it fails.
Execution Table
StepInput ValueActionResultNext Step
1"123"parseInt("123")123 (number)Pass to controller
2"abc"parseInt("abc")NaNThrow BadRequestException
3nullparseInt(null)0Pass to controller
4"42abc"parseInt("42abc")42 (number)Pass to controller
💡 Execution stops when input is valid and passed or invalid and error thrown.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
valueundefined"123""abc"null"42abc"
valundefined123NaN042
Key Moments - 3 Insights
Why does parseInt("42abc") return 42 and not NaN?
parseInt reads numbers from the start of the string until it hits a non-digit. So "42abc" becomes 42. See execution_table row 4.
What happens if the pipe throws an error?
The request stops and NestJS sends a 400 Bad Request response. This is shown in execution_table rows 2.
Why do we return the parsed value instead of the original?
Returning the parsed value ensures the controller gets the correct data type. See execution_table row 1 where "123" becomes 123.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of parsing "abc" at step 2?
ANaN
B123
Cabc
D42
💡 Hint
Check the 'Result' column in execution_table row 2.
At which step does the pipe pass the value to the controller without error?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for 'Pass to controller' in the 'Next Step' column.
If the input was "100xyz", what would the pipe return?
ANaN
B100
C"100xyz"
DThrow error
💡 Hint
Refer to how parseInt handles strings with numbers followed by letters, like in step 4.
Concept Snapshot
Custom pipes in NestJS transform or validate data before it reaches controllers.
They implement PipeTransform with a transform() method.
If validation fails, throw an exception to stop the request.
Return the transformed value to pass it on.
Useful for type conversion and input checks.
Full Transcript
Custom pipes in NestJS run when a request comes in. They check or change the input data before the controller uses it. For example, a pipe can convert a string to a number. If the input is not valid, the pipe throws an error and stops the request. Otherwise, it returns the new value. This helps keep controllers clean and safe. The example pipe tries to parse an integer. If parsing fails, it throws a BadRequestException. If it succeeds, it passes the number to the controller. This process is shown step-by-step in the execution table and variable tracker.