Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJs - Class-validator not returning full validation error object

I am using class-validator in NestJS to create valdations like this:

export class LoginDTO {
@IsEmail()
@MinLength(4)
email: string;

@IsNotEmpty()
@MinLength(4)
password: string;

}

It works, but not as expected. The returned object looks like this :

{
"statusCode": 400,
"message": [
    "email must be longer than or equal to 4 characters",
    "email must be an email"
],
"error": "Bad Request"

}

While i want it to contain all the information like this :

{
    "statusCode": 400,
    [{
        target: /* post object */,
        property: "title",
        value: "Hello",
        constraints: {
        length: "$property must be longer than or equal to 10 characters"
    }]
    "error": "Bad Request"
}

How to do to return all the missing properties ?

like image 445
Waray Mohamed Amine Avatar asked Nov 08 '25 16:11

Waray Mohamed Amine


1 Answers

This was a breaking change in Nestv7. From the migration guide when using the ValidationPipe you can pass an exceptionFactory property like this

exceptionFactory: (errors) => new BadRequestException(errors),

and it should give you what you want.

like image 190
Jay McDoniel Avatar answered Nov 10 '25 16:11

Jay McDoniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!