user5740953
user5740953

Reputation: 179

Nestjs 7 upgrade class-validator issue

We migrated out app to NestJS 7 version, after that all the class validations error just returns one type of error.

{
  "code": 400,
  "message": "Bad Request",
  "detail": "Bad Request Exception"
} 

Before the upgrade it was throwing proper error messages for any class volitions. App boot level configuration is as follows. I have also upgraded the class-validator and class-transfoer packages to the latest.

 const validationPipeOptions: ValidationPipeOptions = {
        disableErrorMessages: false, 
        whitelist: true, 
        dismissDefaultMessages: false,
        forbidNonWhitelisted: false,
        forbidUnknownValues: true,
        skipMissingProperties: false,
        transform: true, 
    };
    app.useGlobalPipes(new ValidationPipe(validationPipeOptions));

Error before the upgrade

{
    "code": 400,
    "message": "Bad Request",
    "detail": [
        {
            "target": {
                "name": 4
            }
        },
        "value": 4,
        "property": "name",
        "constraints": {
            "isString": "name must be a string"
        }
    ]
}

Upvotes: 0

Views: 663

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70161

As mentioned in the migration guide from v6 to v7, if you like the old error message where it gave the details in the format above, you can set the exceptionFactory property to excpetionFactory: (errors) => new BadRequestException(errors)

Upvotes: 1

Related Questions