[ ] 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.
@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;
}
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.
CF code above
Nest version: 5.0.1
For Tooling issues:
- Node version: 10
- Platform: Linux
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.
Most helpful comment
@maxime1992 it's perfectly fine.
This exception is thrown from
class-validator
when the values passed in isundefined
/null
. I'll add a small improvement soon. At this point, you may catch this issue with the exception filter.