简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM