Closed
Description
Description
Using @Type(() => Boolean
does not work as advertised here.
Minimal code-snippet showcasing the problem
I have a minimal app to show the whole scenario https://github.com/somehowchris/nestjs-query-validation-failing
Doing:
curl --location --request POST 'http://localhost:3001/?name=Chris&external=false&age=12' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"Chris",
"external": "false",
"age": "12"
}'
Should transform "false" to the type Boolean with false
import { Type } from 'class-transformer';
import { IsBoolean, IsInt, IsPositive, IsString } from 'class-validator';
export class AppSearchQuery {
@IsString()
public name: string;
@IsBoolean()
@Type(() => Boolean)
public external: boolean;
@IsInt()
@IsPositive()
@Type(() => Number)
public age: number;
}
Which again should result in the return body
{
"name": "Chris",
"external": false,
"age": 12
}
but results in this
{
"name": "Chris",
"external": true,
"age": 12
}
Expected behavior
The boolean should be transformed with its actual value.
Actual behavior
As shown the boolean value is true which should be false.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
diffy0712 commentedon May 14, 2024
Hello @somehowchris,
This works as expected. Any non-empty string value is transformed to boolean true by Javascript.
You can force the 'correct' behavior, that you expect with a custom transformer.
Example:
There is a feature issue on
allow casting of primitive values during transformation
#550.github-actions commentedon Jun 14, 2024
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.