Bird
0
0

You want to create a FileValidationPipe that rejects files if they are not images or exceed 2MB. Which approach correctly combines these validations?

hard📝 Conceptual Q8 of 15
NestJS - Pipes
You want to create a FileValidationPipe that rejects files if they are not images or exceed 2MB. Which approach correctly combines these validations?
AOnly check file size, ignoring file type
BCheck file extension against ['jpg','png','gif'] and file.size <= 2 * 1024 * 1024, else throw exception
CCheck file extension but accept any size
DCompress files larger than 2MB automatically
Step-by-Step Solution
Solution:
  1. Step 1: Define validation rules for type and size

    Allowed extensions are images; size must be 2MB or less.
  2. Step 2: Combine checks logically in transform method

    If either check fails, throw an exception; otherwise, return the file.
  3. Final Answer:

    Check file extension against ['jpg','png','gif'] and file.size <= 2 * 1024 * 1024, else throw exception -> Option B
  4. Quick Check:

    Combine type and size checks in validation [OK]
Quick Trick: Validate both file type and size together [OK]
Common Mistakes:
  • Validating only one condition
  • Assuming automatic compression
  • Ignoring file type validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes