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 · 5 comments
Closed

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

maxime1992 opened this issue May 28, 2018 · 5 comments

Comments

@maxime1992
Copy link

maxime1992 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
Copy link

AntoineMaitre 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
Copy link
Author

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
Copy link
Member

@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
Copy link
Member

Fixed in the v5.1.0

@lock
Copy link

lock bot commented Sep 24, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants