'How to get multiple exceptions with NESTJS?

I would like to know if there are any examples of how to format the returns this way.

{
  "errors": {
    "due_date": [
      "não pode ficar em branco",
      "não pode estar mais que três anos a frente",
      "não pode estar no passado"
    ]
  }
}

I currently handle my exceptions this way

if (!contract.title) {
    throw new BadRequestException('Title is a required field');
}
if (contract.phase in Phase === false) {
     throw new BadRequestException(
       'Contract phase informed is invalid or non-existent',
    );
}

Grateful...



Solution 1:[1]

You can use class-transformer package and add a global ValidationPipe in the main.ts file to validate all the request with a body which has corresponding dto's

Ref: How to add Validation to request body in NestJS

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sathya