Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty @Body() ends up with error 500 #734

Closed
maxime1992 opened this issue May 28, 2018 · 4 comments

Comments

@maxime1992
Copy link

commented May 28, 2018

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
@AntoineMaitre

This comment has been minimized.

Copy link

commented May 28, 2018

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.

@maxime1992

This comment has been minimized.

Copy link
Author

commented May 28, 2018

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?

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented May 28, 2018

@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.

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Jul 5, 2018

Fixed in the v5.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.