Kind: Class
Source: packages/common/pipes/file/file-type.validator.ts
Part of: Common
Defines the built-in FileTypeValidator. It validates incoming files by examining their magic numbers using the file-type package, providing more reliable file type validation than just checking the mimetype string.
FileTypeValidator is a built-in file validation class used with NestJS file upload pipes. It inspects the uploaded file buffer for magic numbers via the file-type package, making validation more reliable than trusting the client-provided MIME type alone.
Extends: FileValidator
Methods
| Method | Signature | Returns |
|---|---|---|
buildErrorMessage | buildErrorMessage(file: IFile) | string |
isValid | isValid(file: IFile) | Promise<boolean> |
Where it refuses work
FileTypeValidatorstops the work with an early return whenthis.validationOptions.fallbackToMimetype, in 4 places.FileTypeValidatorstops the work with an early return whenerrorMessage.FileTypeValidatorstops the work with an early return whenfile?.mimetype && !file.buffer && !this.validationOptions?.fallbackToMimetype && !this.va….FileTypeValidatorstops the work with an early return when!this.validationOptions.FileTypeValidatorstops the work with an early return whenthis.validationOptions.skipMagicNumbersValidation.FileTypeValidatorstops the work with an early return when!isFileValid.
When something fails
FileTypeValidatorhandles failure in 2 places: it logs it and continues in 1, and turns it into a return value in 1.
Diagram
mermaidgraph LR A[Uploaded file] --> B[ParseFilePipe] B --> C[FileTypeValidator] C --> D[Inspect file buffer magic numbers] D --> E[file-type detects MIME type] E --> F{Matches configured fileType?} F -->|Yes| G[Accept file] F -->|No| H[Return validation error]
Usage
tsimport {
Controller,
HttpStatus,
ParseFilePipe,
Post,
UploadedFile,
UseInterceptors,
FileTypeValidator,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
@Controller('uploads')
export class UploadController {
@Post('image')
@UseInterceptors(FileInterceptor('file'))
uploadImage(
@UploadedFile(
new ParseFilePipe({
validators: [
new FileTypeValidator({
fileType: /image\/(jpeg|png)/,
}),
],
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
)
file: Express.Multer.File,
) {
return {
filename: file.originalname,
detectedType: file.mimetype,
};
}
}
AI Coding Instructions
- Use
FileTypeValidatorinsideParseFilePipevalidator lists for uploaded files handled by Multer interceptors. - Configure
fileTypewith either an exact MIME type string, such asimage/jpeg, or a regular expression for multiple allowed types. - Do not rely only on
file.mimetype; this validator checks the file buffer contents and is intended to prevent spoofed MIME types. - Ensure the upload adapter provides a
bufferon the uploaded file, since magic-number detection requires access to file contents. - Combine this validator with
MaxFileSizeValidatorwhen endpoints need both type and size restrictions.
Was this page helpful?