Nest: Empty @Body() ends up with error 500

Created on 28 May 2018  路  5Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ? ] Bug report
[ ? ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

@Post()
method(@Body() myBody: MyBodyDto) {
  console.log(myBody);
}

Making a POST without passing any body ends up with an error 500 and the following stacktrace:

TypeError: Cannot read property 'constructor' of undefined
    at ValidationExecutor.execute (/home/maxime/Documents/perso/my-project/backend/src/validation/ValidationExecutor.ts:44:90)
    at Validator.coreValidate (/home/maxime/Documents/perso/my-project/backend/src/validation/Validator.ts:31:18)
    at Validator.validate (/home/maxime/Documents/perso/my-project/backend/src/validation/Validator.ts:56:21)
    at Object.validate (/home/maxime/Documents/perso/my-project/backend/src/index.ts:49:44)
    at ValidationPipe.transform (/home/maxime/Documents/perso/my-project/backend/node_modules/@nestjs/common/pipes/validation.pipe.js:43:45)
    at transforms.reduce (/home/maxime/Documents/perso/my-project/backend/node_modules/@nestjs/core/pipes/pipes-consumer.js:15:28)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

Note: I'm using class-transformer and class-validator. My DTO is like that for example:

export class MyBodyDto {
  @IsUUID('4')
  @IsNotEmpty()
  id: string;
}

Expected behavior

If an argument is not optional and is not present when a request arrives, the app shouldn't crash and end up with a 500 error.

Minimal reproduction of the problem with instructions

CF code above

Environment


Nest version: 5.0.1


For Tooling issues:
- Node version: 10
- Platform:  Linux
common done 馃憦 type

Most helpful comment

@maxime1992 it's perfectly fine.
This exception is thrown from class-validator when the values passed in is undefined/null. I'll add a small improvement soon. At this point, you may catch this issue with the exception filter.

All 5 comments

Are you using any ValidationPipe ?
You might want to catch and throw a BadRequestException if there's any error from class-validator within this validation pipe.

Oh. Yes I am.

app.useGlobalPipes(new ValidationPipe());

I guess it's the right way to do so. That said, I'm using the one offered by Nest. Should it be done directly there?

@maxime1992 it's perfectly fine.
This exception is thrown from class-validator when the values passed in is undefined/null. I'll add a small improvement soon. At this point, you may catch this issue with the exception filter.

Fixed in the v5.1.0

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

2233322 picture 2233322  路  3Comments

janckerchen picture janckerchen  路  3Comments

mishelashala picture mishelashala  路  3Comments

hackboy picture hackboy  路  3Comments

VRspace4 picture VRspace4  路  3Comments