4

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 ?

1 Answer 1

21

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.