Challenge - 5 Problems
File Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Think about what happens when the file size condition fails inside the pipe.
✗ Incorrect
The pipe checks the file size and throws a BadRequestException if the file is too large, stopping the request with an error.
📝 Syntax
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Check the syntax of the includes method call.
✗ Incorrect
Option C is missing parentheses around the argument to includes, causing a syntax error.
❓ state_output
advanced2: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 }; }
Attempts:
2 left
💡 Hint
The pipe returns the file object, so the controller receives it.
✗ Incorrect
The pipe returns the validated file object, so the controller gets the file with its properties.
🔧 Debug
advanced2: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'); } }
Attempts:
2 left
💡 Hint
Check how indexOf behaves when the item is not found.
✗ Incorrect
indexOf returns -1 when the item is not found, which is truthy in JavaScript, so the if condition incorrectly passes for invalid types.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about code organization and reuse.
✗ Incorrect
Using a pipe keeps validation separate from business logic, making the code easier to maintain and reuse.