Clean up your NestJS Controller: Remote API Validation inside DTOs 🧼🚀
dev.to·21h·
Discuss: DEV
🛡️Error Handling
Preview
Report Post

The "Remote Validation" Struggle

Hey everyone! 👋

If you work with NestJS, you probably love class-validator. Putting an @IsEmail() decorator on a property and sleeping soundly knowing bad data won’t reach your Service is a great feeling.

But that peace of mind usually vanishes when we need Remote Validation.

You know the scenario:

  1. The frontend sends a userId, couponCode, or transactionId.
  2. You need to hit an external Microservice or API to check if it’s valid.
  3. If it’s valid, you often need to fetch extra data (like the user’s name) to save it locally.

The "Dirty" Way (We’ve all done this):

// ❌ The common anti-pattern
@Post()
async create(@Body() dto: CreateOrderDto) {
// Validation logic leaking into the Controller/Service
const res...

Similar Posts

Loading similar posts...