fix: bug in casting boolean properties from `@Query()` in Nest.js
sebasmrl opened this issue · 4 comments
Description
Exist a bug with boolean transformers, when you try casting boolean property from @query() in Nest.js @isboolean() it does not work correctly, i tried doing with @Transfromer() and its execution callback repeat 2 times if you use others decorators, it's supposed to work using @IsBooolean() and @IsOptional() but is not.I was able to solve it doing a OR condition with @Tranformer()
Minimal code-snippet showcasing the problem
//Controller method
@Get()
findAll( @Query() paginationDto: PaginationDto) {
return this.usersService.findAll(paginationDto);
}
//Bug example code when you trying to read
export class PaginationDto{
@IsOptional()
@IsBoolean()
activeRegisters?:boolean;
}
//Momentary solution
export class PaginationDto{
@IsOptional()
@IsBoolean()
@Transform(({ value} ) => {
console.log("TRANSFORM: ",typeof value, value)
return (value == 'false' || value==false) ? false: true;
} )
activeRegisters?:boolean;
}
Expected behavior
it's supposed to work using @IsBooolean() and @IsOptional() but is not.
Actual behavior
In all other cases it works perfectly, only with @query() results have problems
Hello,
could you please describe your problem a bit more? I do not fully understand your problem. I do think you are mixing some things up here. The @IsBooolean()
and @IsOptional()
are decorators for class-validator and not this class-transformer
library.
Neither class-transformer
nor class-validator
has @Query
decorator, so I assume by your example that that is some framework's decorator (nest?).
Does the @Query
decorator call the plainToInstance
or other class-transformer
functions?
The issue does not contain a simple reproduction case.
Closing as invalid.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.