0
0
NestJSframework~10 mins

ValidationPipe in depth in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ValidationPipe in depth
Request Received
Apply ValidationPipe
Transform Input to DTO
Validate DTO Properties
Send Response
The ValidationPipe intercepts incoming requests, transforms and validates data against DTO rules, then either passes valid data to the controller or throws errors for invalid input.
Execution Sample
NestJS
import { ValidationPipe } from '@nestjs/common';

app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
This code sets up a global ValidationPipe that automatically validates and strips unwanted properties from incoming requests.
Execution Table
StepActionInput DataValidation ResultOutcome
1Request received{"name":"John","age":30}Not validated yetProceed to ValidationPipe
2Transform input to DTO{"name":"John","age":30}DTO created with name and ageReady for validation
3Validate DTO properties{"name":"John","age":30}All properties validPass data to controller
4Controller handles data{"name":"John","age":30}No validation errorsProcess request normally
5Request received{"name":"John","age":"thirty"}Not validated yetProceed to ValidationPipe
6Transform input to DTO{"name":"John","age":"thirty"}DTO created with name and ageReady for validation
7Validate DTO properties{"name":"John","age":"thirty"}Age is not a numberThrow validation exception
8Send error response{"name":"John","age":"thirty"}Validation failedRespond with 400 Bad Request
💡 Execution stops when validation fails or data passes successfully to the controller.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 7Final
inputDataundefined{"name":"John","age":30}{"name":"John","age":30}{"name":"John","age":"thirty"}Depends on validation result
validationResultundefinedpendingvalidinvalidvalid or error thrown
errorThrownfalsefalsefalsetruefalse or true
Key Moments - 3 Insights
Why does ValidationPipe throw an error when age is a string instead of a number?
Because the DTO expects age as a number, and ValidationPipe checks types strictly (see execution_table step 7). If the type doesn't match, it throws an error.
What does the whitelist option do in ValidationPipe?
It removes any properties not defined in the DTO from the incoming data before validation, ensuring only expected data is processed (refer to execution_sample code).
When does the controller receive the data?
Only after ValidationPipe validates the input successfully without errors (see execution_table step 4). If validation fails, the controller is not called.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result at step 3?
ANot validated yet
BAll properties valid
CAge is not a number
DValidation failed
💡 Hint
Check the 'Validation Result' column at step 3 in the execution_table.
At which step does the ValidationPipe throw a validation exception?
AStep 7
BStep 4
CStep 2
DStep 1
💡 Hint
Look for the step where 'Throw validation exception' appears in the 'Outcome' column.
If the whitelist option is enabled, what happens to extra properties not in the DTO?
AThey are kept and passed to the controller
BThey cause validation to fail
CThey are removed before validation
DThey are converted to null
💡 Hint
Refer to the execution_sample description about the whitelist option.
Concept Snapshot
ValidationPipe intercepts incoming requests
Transforms input to DTO instances
Validates DTO properties strictly
Throws errors on invalid data
Whitelist option removes unexpected properties
Passes valid data to controller
Full Transcript
ValidationPipe in NestJS is a tool that checks incoming request data against defined rules in DTO classes. When a request arrives, ValidationPipe transforms the raw input into a DTO object. It then validates each property according to the DTO's rules, such as type and required fields. If the data is valid, it passes the cleaned data to the controller to handle. If invalid, it throws an error and sends a 400 Bad Request response. The whitelist option helps by removing any extra properties not defined in the DTO, keeping data clean and safe. This process ensures your app only works with correct and expected data.