3

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

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

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

}

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

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

}

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

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

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

4

1 回答 1

13

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

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

它应该给你你想要的。

于 2020-05-22T15:41:40.173 回答