ValidationPipe helps check if the data sent to your app is correct and safe. It stops bad data before it causes problems.
ValidationPipe setup in NestJS
app.useGlobalPipes(new ValidationPipe({ options }))You add ValidationPipe globally in your main.ts file to check all incoming requests.
You can pass options to customize behavior, like whitelist or transform.
app.useGlobalPipes(new ValidationPipe())
app.useGlobalPipes(new ValidationPipe({ whitelist: true }))app.useGlobalPipes(new ValidationPipe({ transform: true }))app.useGlobalPipes(new ValidationPipe({ forbidNonWhitelisted: true, whitelist: true }))This code sets up a NestJS app and adds ValidationPipe globally. It removes extra properties and converts input types automatically.
import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })); await app.listen(3000); } bootstrap();
Always create DTO classes with validation decorators like @IsString(), @IsInt() to use ValidationPipe effectively.
Use whitelist: true to keep only allowed properties and improve security.
Use transform: true to get correct data types without manual conversion.
ValidationPipe checks and cleans incoming data automatically.
Set it up globally in main.ts for all routes.
Use options like whitelist and transform to customize validation behavior.
Practice
ValidationPipe in a NestJS application?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 CQuick Check:
ValidationPipe purpose = data validation and cleaning [OK]
- Confusing ValidationPipe with authentication features
- Thinking ValidationPipe manages database connections
- Assuming ValidationPipe serves static files
ValidationPipe globally in a NestJS app inside main.ts?Solution
Step 1: Recall NestJS global pipe method
The official method to set global pipes isapp.useGlobalPipes().Step 2: Check other options
Methods likeusePipes,setGlobalPipes, orapplyGlobalPipesdo not exist or are incorrect.Final Answer:
app.useGlobalPipes(new ValidationPipe()) -> Option AQuick Check:
Global pipe setup = useGlobalPipes() [OK]
- Using non-existent methods like setGlobalPipes
- Confusing useGlobalPipes with usePipes
- Missing the 'new' keyword before ValidationPipe
main.ts snippet, what will happen when a request sends extra properties not defined in the DTO?app.useGlobalPipes(new ValidationPipe({ whitelist: true }));Solution
Step 1: Understand whitelist option in ValidationPipe
Settingwhitelist: trueremoves any properties not defined in the DTO from the incoming data.Step 2: Check behavior on extra properties
With whitelist true, extra properties are stripped, not causing errors or crashes.Final Answer:
Extra properties will be removed from the incoming data -> Option BQuick Check:
whitelist true = remove extra props [OK]
- Thinking whitelist causes validation errors on extra props
- Assuming extra props are kept unchanged
- Believing app crashes on extra properties
main.ts code snippet for setting up ValidationPipe globally?app.useGlobalPipes(ValidationPipe({ transform: true }));Solution
Step 1: Check ValidationPipe instantiation
ValidationPipe is a class and must be instantiated with thenewkeyword.Step 2: Validate other parts
Thetransformoption is valid, anduseGlobalPipesis correctly used inmain.ts.Final Answer:
Missing 'new' keyword before ValidationPipe -> Option AQuick Check:
Instantiate ValidationPipe with new [OK]
- Omitting 'new' keyword when creating ValidationPipe
- Thinking transform option is invalid
- Trying to set global pipes inside controllers
Solution
Step 1: Understand transform option
Settingtransform: truetells ValidationPipe to convert incoming payloads to the types defined in DTO classes automatically.Step 2: Check other options
whitelistremoves extra properties,forbidNonWhitelistedthrows errors on extra props, anddisableErrorMessageshides validation errors; none convert types.Final Answer:
transform: true -> Option DQuick Check:
transform true = auto type conversion [OK]
- Confusing whitelist with type conversion
- Thinking forbidNonWhitelisted converts types
- Believing disableErrorMessages affects data types
Ready to prove it?
Pass the test and this pattern moves unknown
