What if you could write your validation rules once and have them magically work everywhere?
Why Custom validation decorators in NestJS? - Purpose & Use Cases
Imagine you have many forms in your app, and you need to check if user inputs follow specific rules like "must be a valid phone number" or "password must have special characters".
You write the same validation code again and again in every controller or service.
Manually repeating validation code everywhere is tiring and easy to forget or make mistakes.
It also makes your code messy and hard to update if rules change.
Custom validation decorators let you write your validation logic once and reuse it everywhere by just adding a simple tag on your data fields.
This keeps your code clean, consistent, and easy to maintain.
if (!isValidPhone(user.phone)) { throw new Error('Invalid phone'); }
@IsPhoneNumber() phone: string;
You can create clear, reusable, and easy-to-read validation rules that automatically check user input wherever needed.
In a signup form, instead of writing phone number checks in the controller, you just add @IsPhoneNumber() on the phone field in your DTO, and NestJS handles the rest.
Manual validation is repetitive and error-prone.
Custom decorators let you write validation once and reuse it.
This makes your code cleaner and easier to maintain.