Skip to content

fix: IsDefined + IsOptional combination return error 500 instead of 400 (bad request) #1492

@luanpersini

Description

@luanpersini

Description

I have a DTO class with a param that is an object. Ive set it to defined and optional. Somehow, if i remove the optional, the validation goes ok (returns 400), if i keep the isOptional, it returns error 500. Its not validating as expected and the application cannot deal with the null value.

Minimal code-snippet showcasing the problem

 @IsDefined()
 @IsNotEmptyObject()
 @IsObject()
 @ValidateNested()
 @ApiProperty({
    example: AccountDto,
    description: 'Account information.',
  })
  @Type(() => AccountDto)
  @IsOptional()
  account!: AccountDto;
 //request
 
{
account: null
}

Expected behavior

Should return error 400 bad request, since its undefined.

Actual behavior

It throws internal server error (isnt returning the error 400)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions