A custom pipe in NestJS is a class that implements the PipeTransform interface to transform or validate data before it reaches the route handler.
Click to reveal answer
beginner
Which method must be implemented in a custom pipe class?
The method transform(value: any, metadata: ArgumentMetadata) must be implemented to define how the input data is processed.
Click to reveal answer
beginner
How do you apply a custom pipe to a route handler parameter?
You apply a custom pipe by adding it as an argument decorator, for example: @Param('id', new MyCustomPipe()) id: string.
Click to reveal answer
intermediate
What happens if a custom pipe throws an exception during transformation?
If a custom pipe throws an exception, NestJS stops the request processing and sends an error response to the client.
Click to reveal answer
intermediate
Why use custom pipes instead of handling validation inside controllers?
Custom pipes keep controllers clean by separating validation and transformation logic, making code easier to maintain and reuse.
Click to reveal answer
What interface must a custom pipe implement in NestJS?
APipeTransform
BPipeInterface
CTransformable
DValidator
✗ Incorrect
Custom pipes must implement the PipeTransform interface to define the transform method.
Where is the transform method called in a custom pipe?
ADuring module initialization
BAfter the response is sent
CBefore the route handler executes
DInside the controller constructor
✗ Incorrect
The transform method runs before the route handler to process incoming data.
How do you apply a custom pipe globally in NestJS?
AImport it in the module providers
BAdd it to the controller constructor
CUse @GlobalPipe decorator
DUse app.useGlobalPipes(new MyPipe())
✗ Incorrect
Global pipes are applied with app.useGlobalPipes() in the main bootstrap file.
What should a custom pipe return if the input is valid?
AThrow an exception
BThe transformed or original value
CNull
DUndefined
✗ Incorrect
If valid, the pipe returns the transformed or original value to continue processing.
Which decorator is commonly used to apply a pipe to a route parameter?
AAll of the above
B@Body
C@Query
D@Param
✗ Incorrect
Pipes can be applied to @Param, @Body, and @Query decorators to validate or transform data.
Explain how to create and use a custom pipe in NestJS.
Think about how data flows before reaching the controller.
You got /4 concepts.
Describe the benefits of using custom pipes for validation and transformation.
Consider how code organization improves with pipes.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of a custom pipe in NestJS?
easy
A. To transform or validate data before it reaches the route handler
B. To handle database connections automatically
C. To manage user sessions and cookies
D. To style the frontend components
Solution
Step 1: Understand the role of pipes in NestJS
Pipes are used to transform or validate incoming data before it reaches the controller methods.
Step 2: Identify what custom pipes specifically do
Custom pipes allow developers to create their own logic for data transformation or validation to ensure data safety and correctness.
Final Answer:
To transform or validate data before it reaches the route handler -> Option A
Quick Check:
Custom pipes = data validation/transformation [OK]
Hint: Pipes act on data before controller uses it [OK]
Common Mistakes:
Confusing pipes with middleware
Thinking pipes handle database or session logic
Assuming pipes style frontend components
2.
Which of the following is the correct way to define a custom pipe class in NestJS?
export class MyPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
// logic here
}
}
easy
A. export class MyPipe implements PipeTransform { transform(value: any) { } }
B. export class MyPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { } }
C. export function MyPipe(value: any) { return value; }
D. export class MyPipe { transform(value: any, metadata: ArgumentMetadata) { } }
Solution
Step 1: Recall the PipeTransform interface signature
The transform method must accept two parameters: value and metadata of type ArgumentMetadata.
Step 2: Check which option matches the correct method signature
export class MyPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { } } correctly implements the transform method with both parameters and the class implements PipeTransform.
Final Answer:
export class MyPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { } } -> Option B
The method returns value.toUpperCase without parentheses, which is a reference to the function, not the result.
Step 2: Identify correct usage of toUpperCase
toUpperCase is a method and must be called with parentheses: value.toUpperCase()
Final Answer:
toUpperCase is used without parentheses -> Option C
Quick Check:
Method calls need () to execute [OK]
Hint: Remember to call string methods with () [OK]
Common Mistakes:
Forgetting parentheses on method calls
Assuming decorators are missing when present
Ignoring interface implementation
5.
You want to create a custom pipe that trims whitespace and converts input to lowercase. Which of these implementations correctly achieves this?
import { PipeTransform, Injectable } from '@nestjs/common';
@Injectable()
export class NormalizePipe implements PipeTransform {
transform(value: any) {
// Which option is correct?
}
}
hard
A. return value.trim().toLowerCase();
B. return value.replace(/\s/g, '').toLowerCase();
C. return value.trim.toLowerCase();
D. return value.toLowerCase.trim();
Solution
Step 1: Understand method chaining order
trim() removes whitespace from start and end, then toLowerCase() converts to lowercase. The order matters to avoid errors.
Step 2: Check method call syntax
Both trim() and toLowerCase() are methods and must be called with parentheses. return value.trim().toLowerCase(); correctly calls them in order: value.trim().toLowerCase()
Final Answer:
return value.trim().toLowerCase(); -> Option A
Quick Check:
Correct method chaining with () = return value.trim().toLowerCase(); [OK]
Hint: Call trim() before toLowerCase(), both with () [OK]
Common Mistakes:
Calling methods without parentheses
Wrong method chaining order
Using properties instead of methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.