Discover how to make your API data handling effortless and safe with just a simple setting!
Why Whitelist and transform options in NestJS? - Purpose & Use Cases
Imagine building an API where users send data, and you manually check each field to accept or reject it.
You also manually convert data types like strings to numbers everywhere in your code.
This manual checking and converting is slow, repetitive, and easy to forget.
It leads to bugs, security holes, and messy code that is hard to maintain.
Whitelist and transform options in NestJS automatically allow only expected fields and convert data types for you.
This keeps your code clean, safe, and consistent without extra effort.
if (typeof data.age === 'string') { data.age = parseInt(data.age); } for (const key in data) { if (!['name', 'age'].includes(key)) { delete data[key]; } }
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));You can trust incoming data is clean and correctly typed, so your app runs smoothly and securely.
When a user submits a form with extra fields or wrong types, NestJS strips unwanted fields and converts types automatically before your code uses the data.
Manual data checks are error-prone and tedious.
Whitelist option removes unexpected fields automatically.
Transform option converts data types for you.