Bird
0
0

Given this FileValidationPipe code snippet, what happens if a user uploads a file with an unsupported extension?

medium📝 Predict Output Q4 of 15
NestJS - Pipes
Given this FileValidationPipe code snippet, what happens if a user uploads a file with an unsupported extension?
class FileValidationPipe implements PipeTransform {
  transform(file) {
    const allowed = ['jpg', 'png'];
    const ext = file.originalname.split('.').pop();
    if (!allowed.includes(ext)) {
      throw new BadRequestException('Invalid file type');
    }
    return file;
  }
}
AThe pipe logs a warning but allows the file
BThe file is accepted without any error
CThe file is renamed to a supported extension
DThe pipe throws a BadRequestException with message 'Invalid file type'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the transform method logic

    The method extracts the file extension and checks if it is in the allowed list.
  2. Step 2: Understand the behavior on invalid extension

    If the extension is not allowed, it throws a BadRequestException with a specific message.
  3. Final Answer:

    The pipe throws a BadRequestException with message 'Invalid file type' -> Option D
  4. Quick Check:

    Invalid extension triggers exception [OK]
Quick Trick: Invalid file types cause exceptions in validation pipes [OK]
Common Mistakes:
  • Assuming invalid files are accepted
  • Thinking files get renamed automatically
  • Believing pipe only logs warnings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes