0
0
NestJSframework~20 mins

File validation pipe in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a file larger than the allowed size is uploaded?
Consider a NestJS file validation pipe that limits file size to 1MB. What is the behavior when a user uploads a file of 2MB?
NestJS
import { Injectable, PipeTransform, BadRequestException } from '@nestjs/common';

@Injectable()
export class FileSizeValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    const maxSize = 1 * 1024 * 1024; // 1MB
    if (file.size > maxSize) {
      throw new BadRequestException('File size exceeds limit');
    }
    return file;
  }
}
AThe pipe throws a BadRequestException with message 'File size exceeds limit'.
BThe pipe returns null and the upload is ignored.
CThe pipe silently accepts the file and returns it without error.
DThe pipe logs a warning but allows the file to pass through.
Attempts:
2 left
💡 Hint
Think about what happens when the file size condition fails inside the pipe.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this file validation pipe code
Which option contains a syntax error preventing the file validation pipe from compiling?
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';

@Injectable()
export class FileTypeValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    const allowedTypes = ['image/png', 'image/jpeg'];
    if (!allowedTypes.includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type');
    }
    return file;
  }
}
Aif (!allowedTypes.includes(file.mimetype)) { throw new BadRequestException('Invalid file type'); }
Bif (!allowedTypes.includes(file.mimetype)) throw new BadRequestException('Invalid file type');
Cif (!allowedTypes.includes file.mimetype) { throw new BadRequestException('Invalid file type'); }
Dif (!allowedTypes.includes(file.mimetype)) { throw new BadRequestException('Invalid file type'); } else return file;
Attempts:
2 left
💡 Hint
Check the syntax of the includes method call.
state_output
advanced
2:00remaining
What is the output when the file validation pipe returns the file object?
Given a file validation pipe that returns the file object after validation, what will be the value passed to the controller handler?
NestJS
import { Injectable, PipeTransform } from '@nestjs/common';

@Injectable()
export class FileValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    // Assume validation passed
    return file;
  }
}

// Controller handler example
async uploadFile(@UploadedFile(new FileValidationPipe()) file: Express.Multer.File) {
  return { filename: file.originalname, size: file.size };
}
Aundefined
B{ filename: 'uploaded_file.png', size: 2048 } // file info object
CAn error is thrown because the pipe does not return a value
Dnull
Attempts:
2 left
💡 Hint
The pipe returns the file object, so the controller receives it.
🔧 Debug
advanced
2:00remaining
Why does this file validation pipe not catch invalid file types?
This pipe is intended to reject files not of type 'image/png' or 'image/jpeg', but it lets invalid files pass. Why?
NestJS
import { Injectable, PipeTransform, BadRequestException } from '@nestjs/common';

@Injectable()
export class FileTypeValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    const allowedTypes = ['image/png', 'image/jpeg'];
    if (allowedTypes.indexOf(file.mimetype)) {
      return file;
    }
    throw new BadRequestException('Invalid file type');
  }
}
AThe file.mimetype property is undefined, so the check fails silently.
BThe allowedTypes array is empty, so no types are allowed.
CThe pipe does not throw any error because transform is not called.
DindexOf returns -1 for missing items, which is truthy, so the condition is wrong.
Attempts:
2 left
💡 Hint
Check how indexOf behaves when the item is not found.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using a file validation pipe in NestJS?
Why should you use a dedicated file validation pipe instead of validating files directly inside controller methods?
AIt centralizes validation logic, making code cleaner and reusable across multiple routes.
BIt automatically compresses files before saving them.
CIt prevents the need to import Multer in the controller.
DIt allows files to be uploaded without any size or type restrictions.
Attempts:
2 left
💡 Hint
Think about code organization and reuse.