Questions
7 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?
07 / 29

How do you write a custom pipe that sanitizes a string input (trim and lowercase) in NestJS?

Implement PipeTransform with string generic types, guard against non-string inputs with a typeof check, and return the sanitized value. Apply at param level for targeted sanitization or at handler level with @UsePipes() for all string arguments of that route.

SanitizeStringPipe implementation and usage
Custom pipe design best practices:
  1. 1

    Always guard non-target types with typeof or metadata.metatype checks — pass them through unchanged.

  2. 2

    Use PipeTransform<Input, Output> generics to make the transformation contract explicit.

  3. 3

    Param-level application is the most precise — only the targeted argument is processed.

  4. 4

    Handler-level @UsePipes() applies to all arguments — useful for bulk sanitization.

  5. 5

    Stateless pipes (no injected services) can be used as class references without new.

Difficulty: 5/10
Topics: pipes, validation, string sanitization

Scenario Questions

0-2 years experience
  1. 1

    Can you walk me through how you'd implement a NestJS pipe that trims whitespace and lowercases a string coming from a request body?

  2. 2

    If you applied your pipe to a DTO property that sometimes receives null, what would happen and how would you handle it?

  3. 3

    Where would you register this pipe if you only want it to affect a single controller method?

2-5 years experience
  1. 1

    Suppose you added this sanitizing pipe globally, but some endpoints expecting case‑sensitive strings (like API keys) are now failing. How would you troubleshoot and resolve the issue?

  2. 2

    Your team wants the pipe to also reject strings that contain prohibited characters. How would you extend the pipe while keeping it reusable?

  3. 3

    During testing, the pipe seems to be skipped when validation fails earlier in the pipeline. Explain why that might happen and how to ensure it runs.

5-8 years experience
  1. 1

    When scaling the service to handle thousands of requests per second, what performance considerations would you evaluate for a globally applied sanitizing pipe?

  2. 2

    How would you design a strategy to version your custom pipe so that older clients can continue using the previous sanitization behavior while new clients get the updated logic?

  3. 3

    If multiple modules need slightly different sanitization rules, how would you structure your pipe architecture to avoid code duplication and maintain consistency?

8+ years experience
  1. 1

    Across several microservices, you need consistent string sanitization but each service uses different NestJS versions. How would you create a shared library for the pipe, handling compatibility and deployment?

  2. 2

    Your organization is moving from a monolith to a distributed system and wants to enforce input sanitization centrally. Discuss the trade‑offs between implementing the pipe at the API gateway versus inside each NestJS service.

  3. 3

    What governance processes would you put in place to ensure any new custom pipes meet security, testing, and performance standards across teams?

Follow-up Questions

  • What edge cases did you consider when writing the pipe?
  • How would you test this pipe in isolation and as part of an integration test?
  • Can you discuss any potential security implications of sanitizing inputs this way?