fix: Cast string to Boolean

See original GitHub issue

Hi, 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:open
  • Created 3 years ago
  • Reactions:8
  • Comments:7

github_iconTop GitHub Comments

4reactions
andrebritocommented, May 12, 2022

Hey, guys. I’m in a similar place. Using @joanna-liana’s answer, I’ve manage to work around like this:

// dto
@IsOptional()
@IsBoolean()
@Transform(({ value }) => {
  if (value === 'true') return true;
  if (value === 'false') return false;
  return value;
})
private: boolean;
// controller
@UsePipes(
  new ValidationPipe({
    always: true,
  }),
)

It’s fishy, but it works.

4reactions
jacobdo2commented, May 11, 2021

“false” is true … In my opinion, this should not be casted, correct type or transform annotation should be used.

@Transform(({ obj }) => {
    return [true, 'enabled', 'true'].indexOf(obj.isAnnotate) > -1;
})

Your statement is “false”

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found