Bird
0
0

Given this pipe code snippet, what will happen if a user uploads a file larger than 1MB?

medium📝 component behavior Q13 of 15
NestJS - Pipes
Given this pipe code snippet, what will happen if a user uploads a file larger than 1MB?
export class FileValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    if (!file) throw new BadRequestException('File is required');
    if (file.size > 1_000_000) throw new BadRequestException('File too large');
    return file;
  }
}
AThe pipe ignores the size and processes the file
BThe pipe accepts the file and returns it
CThe pipe throws a NotFoundException
DThe pipe throws a BadRequestException with message 'File too large'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the size check in the transform method

    The pipe checks if file.size is greater than 1,000,000 bytes (1MB).
  2. Step 2: Understand the exception thrown on size violation

    If the file is too large, it throws BadRequestException('File too large'), stopping the request.
  3. Final Answer:

    The pipe throws a BadRequestException with message 'File too large' -> Option D
  4. Quick Check:

    File size > 1MB = BadRequestException [OK]
Quick Trick: Check exception messages in transform() for validation errors [OK]
Common Mistakes:
  • Assuming the pipe accepts large files
  • Confusing BadRequestException with NotFoundException
  • Ignoring the file presence check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes