Questions
20 of 29
1What is a pipe in NestJS and what are its two primary uses?
2How does ValidationPipe work with class-validator and class-transformer in NestJS?
3What is the difference between applying a pipe at the param level vs handler level vs globally in NestJS?
4How do you write a custom pipe for parsing and validating a MongoDB ObjectId param in NestJS?
5What are all the built-in pipes NestJS ships with?
6What is class-transformer and how does it complement class-validator in NestJS?
7How do you write a custom pipe that sanitizes a string input (trim and lowercase) in NestJS?
8How do you write a pipe that uses metatype to skip validation for primitive types the same way ValidationPipe does?
9What are the two ways to register a global pipe in NestJS and what is the difference?
10What are all the significant ValidationPipe options and what does each do in NestJS?
11How do you unit test a custom pipe in NestJS?
12Can a pipe be asynchronous in NestJS and how do you implement one?
13How do you write an async custom validator that checks uniqueness against a database in NestJS?
14How do you validate arrays and conditional fields with class-validator in NestJS?
15How do you use validation groups with class-validator to apply different rules for create vs update operations in NestJS?
16What does @Exclude() and @Expose() do and how do you use them to strip sensitive fields from responses?
17What is the excludeExtraneousValues option in class-transformer and how does it differ from whitelist in ValidationPipe?
18How do you make a custom pipe configurable by accepting constructor options in NestJS?
19How do you customise the error response format produced by ValidationPipe in NestJS?
20How do you handle file upload validation with ParseFilePipe and custom file validators in NestJS?
21What interface must a custom pipe implement and what does the transform() method receive?
22What is the difference between a transformation pipe and a validation pipe in NestJS?
23How do you pass options to a built-in pipe like ParseIntPipe or ParseUUIDPipe in NestJS?
24What is class-validator and how does it integrate with NestJS ValidationPipe?
25What is the @ValidateNested() decorator and when must you pair it with @Type()?
26How do you write a custom class-validator decorator in NestJS?
27What does the @Transform() decorator do in class-transformer and when should you use it?
28How does @Type() interact with discriminated unions and polymorphic DTOs in class-transformer?
29How do you write a pipe that validates and parses a date string from a route parameter in NestJS?
20 / 29

How do you handle file upload validation with ParseFilePipe and custom file validators in NestJS?

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.

ParseFilePipe with built-in and custom validators
File validation design notes:
  1. 1

    MaxFileSizeValidator checks file.size in bytes — multiply MB by 1024 * 1024.

  2. 2

    FileTypeValidator accepts a regex matched against file.mimetype — not the file extension.

  3. 3

    Custom FileValidator extends the abstract base class and implements isValid() and buildErrorMessage().

  4. 4

    isValid() can be async — useful for reading file headers or dimensions from the buffer.

  5. 5

    errorHttpStatusCode defaults to 400 — use HttpStatus.UNPROCESSABLE_ENTITY (422) for semantic correctness.

  6. 6

    fileIsRequired: false makes the pipe pass through when no file is provided.

Difficulty: 5/10
Topics: ParseFilePipe, custom validators, NestJS file upload

Scenario Questions

0-2 years experience
  1. 1

    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.

  2. 2

    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?

2-5 years experience
  1. 1

    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?

  2. 2

    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?

5-8 years experience
  1. 1

    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?

  2. 2

    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?

8+ years experience
  1. 1

    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?

  2. 2

    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?

Follow-up Questions

  • What does NestJS return to the client when a validator throws an error?
  • How would you unit‑test both built‑in and custom file validators?
  • Can you walk me through the order in which NestJS executes pipes for a multipart request?