0
0
NestJSframework~10 mins

File validation pipe in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File validation pipe
File Upload Request
Pipe Receives File
Validate File Type?
NoThrow Error: Invalid Type
Yes
Validate File Size?
NoThrow Error: File Too Large
Yes
Pass Valid File to Controller
Controller Processes File
The file validation pipe checks the uploaded file's type and size before allowing it to reach the controller.
Execution Sample
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';

@Injectable()
export class FileValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    if (!file) throw new BadRequestException('No file uploaded');
    if (!['image/png', 'image/jpeg'].includes(file.mimetype)) {
      throw new BadRequestException('Invalid file type');
    }
    if (file.size > 1024 * 1024) {
      throw new BadRequestException('File too large');
    }
    return file;
  }
}
This pipe checks if a file exists, then validates its type and size, throwing errors if invalid.
Execution Table
StepActionInput FileCheckResultNext Step
1Receive file{mimetype: 'image/png', size: 500000}File exists?YesValidate type
2Validate typeimage/pngIs type in ['image/png', 'image/jpeg']?YesValidate size
3Validate size500000 bytesIs size <= 1MB?YesReturn file
4ReturnValid fileN/AFile passed validationController receives file
5Receive filenullFile exists?NoThrow error: No file uploaded
6Receive file{mimetype: 'application/pdf', size: 500000}File exists?YesValidate type
7Validate typeapplication/pdfIs type in ['image/png', 'image/jpeg']?NoThrow error: Invalid file type
8Receive file{mimetype: 'image/jpeg', size: 2000000}File exists?YesValidate type
9Validate typeimage/jpegIs type in ['image/png', 'image/jpeg']?YesValidate size
10Validate size2000000 bytesIs size <= 1MB?NoThrow error: File too large
💡 Execution stops when file fails validation or passes all checks successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
fileundefined{mimetype: 'image/png', size: 500000}{mimetype: 'image/png', size: 500000}{mimetype: 'image/png', size: 500000}Returned valid file
fileundefinednullnullnullError thrown: No file uploaded
fileundefined{mimetype: 'application/pdf', size: 500000}{mimetype: 'application/pdf', size: 500000}N/AError thrown: Invalid file type
fileundefined{mimetype: 'image/jpeg', size: 2000000}{mimetype: 'image/jpeg', size: 2000000}{mimetype: 'image/jpeg', size: 2000000}Error thrown: File too large
Key Moments - 3 Insights
Why does the pipe throw an error if the file is null?
Because the pipe first checks if a file exists (Step 1 in execution_table). If the file is null or undefined, it immediately throws a 'No file uploaded' error to prevent further processing.
What happens if the file type is not in the allowed list?
At Step 7 in execution_table, if the file mimetype is not 'image/png' or 'image/jpeg', the pipe throws an 'Invalid file type' error and stops execution.
How does the pipe handle files larger than 1MB?
At Step 10, the pipe checks the file size. If it exceeds 1MB (1024*1024 bytes), it throws a 'File too large' error, preventing the file from reaching the controller.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result at Step 3 when the file size is 500000 bytes?
AFile size is valid, proceed to return file
BFile size is too large, throw error
CFile type is invalid, throw error
DNo file uploaded error
💡 Hint
Check Step 3 row in execution_table for file size validation result.
At which step does the pipe throw an error for an unsupported file type?
AStep 10
BStep 2
CStep 7
DStep 5
💡 Hint
Look for the step where file mimetype is checked against allowed types and error is thrown.
If the file is missing, what will the variable 'file' be at Step 1 and what happens next?
A'file' is valid, pipe continues validation
B'file' is null, pipe throws 'No file uploaded' error
C'file' is undefined, pipe returns file
D'file' is null, pipe accepts file
💡 Hint
Refer to variable_tracker and execution_table Step 5 for file null check.
Concept Snapshot
File Validation Pipe in NestJS:
- Implements PipeTransform interface
- Checks if file exists
- Validates file mimetype against allowed list
- Validates file size limit
- Throws BadRequestException on failure
- Passes valid file to controller
- Ensures safe file uploads
Full Transcript
This visual execution trace shows how a NestJS file validation pipe works step-by-step. When a file upload request arrives, the pipe first checks if a file is present. If not, it throws a 'No file uploaded' error. If a file exists, it checks the file type against allowed types like 'image/png' and 'image/jpeg'. If the type is invalid, it throws an error. Next, it checks the file size to ensure it does not exceed 1MB. If the file is too large, it throws an error. If all checks pass, the pipe returns the valid file to the controller for further processing. The variable tracker shows how the file variable changes or triggers errors at each step. Key moments clarify common confusions about missing files, invalid types, and size limits. The quiz questions help reinforce understanding by referencing specific steps and variable states in the execution table.