NestJS provides ParseFilePipe with built-in MaxFileSizeValidator and FileTypeValidator. Custom validators extend the abstract FileValidator class and implement isValid() and buildErrorMessage(). Pass an array of validator instances to ParseFilePipe's validators option.
MaxFileSizeValidator checks file.size in bytes — multiply MB by 1024 * 1024.
FileTypeValidator accepts a regex matched against file.mimetype — not the file extension.
Custom FileValidator extends the abstract base class and implements isValid() and buildErrorMessage().
isValid() can be async — useful for reading file headers or dimensions from the buffer.
errorHttpStatusCode defaults to 400 — use HttpStatus.UNPROCESSABLE_ENTITY (422) for semantic correctness.
fileIsRequired: false makes the pipe pass through when no file is provided.
You need to accept a profile picture upload in a NestJS controller and ensure it's a JPEG under 2 MB. Walk me through how you'd set up ParseFilePipe and any built‑in validators to enforce this.
If a client sends a file with a .png extension but the MIME type is image/jpeg, what will ParseFilePipe do, and how would you handle that case?
We added a custom validator to reject images that contain EXIF metadata. After deploying, users report that some uploads are failing with a generic 500 error. How would you investigate and fix the issue?
Suppose you need to support both single file and multiple file uploads in the same endpoint, each with different size limits. How would you configure ParseFilePipe and custom validators to handle this ambiguity?
Our service processes thousands of file uploads per minute and we notice the validation step becoming a bottleneck. What strategies would you use to optimize ParseFilePipe validation at scale?
We need to store uploaded files in an S3 bucket but also enforce virus scanning before persisting. How would you integrate a custom async validator with ParseFilePipe while keeping the request responsive?
The company is migrating from a legacy multipart handling library to NestJS's ParseFilePipe across many microservices. What architectural considerations and migration plan would you propose to ensure consistency and minimal downtime?
Different teams have conflicting requirements for file validation (e.g., some need strict type checks, others need flexible size limits). How would you design a shared validation framework using custom validators that can be extended without breaking existing services?