📖 Scenario: You are building a simple NestJS API that accepts user data. To keep your API safe and clean, you want to add validation to the incoming data.
🎯 Goal: Set up a ValidationPipe in your NestJS application to automatically validate incoming requests based on DTO rules.
📋 What You'll Learn
Create a DTO class with validation decorators
Create a ValidationPipe instance with whitelist enabled
Apply the ValidationPipe globally in the main application bootstrap
Ensure the application uses the DTO and validation pipe correctly
💡 Why This Matters
🌍 Real World
ValidationPipe helps keep APIs safe by checking incoming data automatically, preventing bad or unexpected data from causing errors or security issues.
💼 Career
Backend developers often use ValidationPipe in NestJS to enforce data rules and improve API reliability and security.
Progress0 / 4 steps
1
Create a User DTO with validation decorators
Create a class called CreateUserDto with two properties: username and age. Use @IsString() decorator on username and @IsInt() decorator on age from the class-validator package.
NestJS
Hint
Remember to import IsString and IsInt from class-validator.
2
Create a ValidationPipe instance with whitelist enabled
Create a constant called validationPipe and assign it a new ValidationPipe instance with the option whitelist: true.
NestJS
Hint
Use new ValidationPipe({ whitelist: true }) to create the pipe.
3
Apply the ValidationPipe globally in the main bootstrap function
In the bootstrap function, after creating the app with NestFactory.create(AppModule), apply the validationPipe globally using app.useGlobalPipes(validationPipe).
NestJS
Hint
Use app.useGlobalPipes(validationPipe) to apply the pipe globally.
4
Use the CreateUserDto in a controller method
In a controller method, add a parameter called createUserDto of type CreateUserDto decorated with @Body() to receive and validate incoming user data.
NestJS
Hint
Use @Body() createUserDto: CreateUserDto as the method parameter.
Practice
(1/5)
1. What is the main purpose of using ValidationPipe in a NestJS application?
easy
A. To serve static files like images and CSS
B. To handle database connections automatically
C. To check and clean incoming data to ensure it is safe and correct
D. To manage user authentication and authorization
Solution
Step 1: Understand ValidationPipe role
ValidationPipe is designed to validate and sanitize incoming data in NestJS, ensuring the data meets expected rules.
Step 2: Compare with other options
Other options like database handling, authentication, or serving static files are unrelated to ValidationPipe's purpose.
Final Answer:
To check and clean incoming data to ensure it is safe and correct -> Option C
Quick Check:
ValidationPipe purpose = data validation and cleaning [OK]
Hint: ValidationPipe is all about checking incoming data [OK]
Common Mistakes:
Confusing ValidationPipe with authentication features
C. useGlobalPipes should be called inside a controller
D. ValidationPipe cannot be used globally
Solution
Step 1: Check ValidationPipe instantiation
ValidationPipe is a class and must be instantiated with the new keyword.
Step 2: Validate other parts
The transform option is valid, and useGlobalPipes is correctly used in main.ts.
Final Answer:
Missing 'new' keyword before ValidationPipe -> Option A
Quick Check:
Instantiate ValidationPipe with new [OK]
Hint: Always use 'new' with ValidationPipe class [OK]
Common Mistakes:
Omitting 'new' keyword when creating ValidationPipe
Thinking transform option is invalid
Trying to set global pipes inside controllers
5. You want to automatically convert incoming JSON strings to their expected types in your DTOs. Which ValidationPipe option should you enable globally to achieve this?
hard
A. forbidNonWhitelisted: true
B. whitelist: true
C. disableErrorMessages: true
D. transform: true
Solution
Step 1: Understand transform option
Setting transform: true tells ValidationPipe to convert incoming payloads to the types defined in DTO classes automatically.
Step 2: Check other options
whitelist removes extra properties, forbidNonWhitelisted throws errors on extra props, and disableErrorMessages hides validation errors; none convert types.
Final Answer:
transform: true -> Option D
Quick Check:
transform true = auto type conversion [OK]
Hint: Use transform true to convert types automatically [OK]
Common Mistakes:
Confusing whitelist with type conversion
Thinking forbidNonWhitelisted converts types
Believing disableErrorMessages affects data types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.