fix: Cast string to Boolean
See original GitHub issueHi, I use class-transformer on NestJS and a cast problem occured during the conversion of the request payload.
Example :
{
"page": "1",
"limit": "1",
"isAnnotate": "false"
}
We got :
{
page: 1,
limit: 1,
isAnnotate: true
}
The defining class is :
class ArticleQuery {
page: number;
limit: number;
isAnnotate?: boolean
}
https://github.com/nestjs/nest/blob/master/packages/common/pipes/validation.pipe.ts#L96
When the plainToClass function was executed such as :
/*
* metatype => ArticleQuery
* value => { page: "1", limit: "1", isAnnotate: "false" }
* transformOptions => { enableImplicitConversion: true }
* /
classTransformer.plainToClass(metatype, value, this.transformOptions);
After a little digging, i found the caused into the function transform :
https://github.com/typestack/class-transformer/blob/develop/src/TransformOperationExecutor.ts#L108
} else if (targetType === Boolean && !isMap) {
if (value === null || value === undefined) return value;
return Boolean(value);
So i made a patch locally to solve the issue :
} else if (targetType === Boolean && !isMap) {
if (value === null || value === undefined) return value;
return value === "true";
Is it an attended behavior from the function to not convert a string “true”/“false” to a Boolean ?
Current version: 0.4.0
Best regards
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:7
Top Results From Across the Web
Java Convert String to boolean - javatpoint
We can convert String to boolean in java using Boolean.parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.valueOf(string) ...
Read more >Java - Convert String to Boolean Example - Java67
There are two ways to convert a String to a boolean in Java, first, by using Boolean.parseBoolean() method, and second, by using Boolean.valueOf()...
Read more >2 Ways to Convert Values to Boolean in JavaScript
The first ! coerce the value to a boolean and inverse it. In this case, !value will return false . So to reverse...
Read more >How to convert string to boolean JavaScript - Educative.io
In this shot, we will cover how to convert a string into its boolean representation. The easiest way to convert string to boolean...
Read more >How can I convert a string to boolean in JavaScript?
Do: var isTrueSet = (myValue === 'true');. using the identity operator ( === ), which doesn't make any implicit type conversions when the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey, guys. I’m in a similar place. Using @joanna-liana’s answer, I’ve manage to work around like this:
It’s fishy, but it works.
Your statement is “false”