NestJs - 类验证器未返回完整的验证错误对象

War*_*ine 3 validation node.js typescript class-validator nestjs

我在 NestJS 中使用类验证器来创建这样的验证:

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

@IsNotEmpty()
@MinLength(4)
password: string;
Run Code Online (Sandbox Code Playgroud)

}

它有效,但不像预期的那样。返回的对象如下所示:

{
"statusCode": 400,
"message": [
    "email must be longer than or equal to 4 characters",
    "email must be an email"
],
"error": "Bad Request"
Run Code Online (Sandbox Code Playgroud)

}

虽然我希望它包含这样的所有信息:

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

如何返回所有丢失的属性?

Jay*_*iel 9

这是Nestv7. 使用时的迁移指南中,ValidationPipe您可以传递这样的exceptionFactory属性

exceptionFactory: (errors) => new BadRequestException(errors),
Run Code Online (Sandbox Code Playgroud)

它应该给你你想要的。