Bird
0
0

Examine the following custom pipe code. What is the main issue that would prevent it from working correctly?

medium📝 Debug Q6 of 15
NestJS - Pipes

Examine the following custom pipe code. What is the main issue that would prevent it from working correctly?

import { PipeTransform, Injectable } from '@nestjs/common';

@Injectable()
export class LowercasePipe implements PipeTransform {
  transform(value: any) {
    return value.toLowerCase();
  }
}
AThe pipe does not handle non-string inputs, which may cause runtime errors
BThe transform method does not specify the return type
CThe @Injectable decorator is missing
DThe class does not implement the PipeTransform interface
Step-by-Step Solution
Solution:
  1. Step 1: Review the code

    The pipe transforms the input to lowercase using toLowerCase().
  2. Step 2: Identify potential issues

    If the input is not a string, calling toLowerCase() will cause a runtime error.
  3. Step 3: Check other options

    The @Injectable decorator is present, and the class implements PipeTransform. Return type omission is allowed in TypeScript.
  4. Final Answer:

    The pipe does not handle non-string inputs, which may cause runtime errors -> Option A
  5. Quick Check:

    Always validate input types in pipes [OK]
Quick Trick: Check input types before string methods [OK]
Common Mistakes:
  • Ignoring input type validation
  • Assuming all inputs are strings
  • Overlooking runtime errors from invalid inputs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes