Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug/proposal] for boolean prop type and enabled enableImplicitConversion flag #306

Closed
ruscon opened this issue Nov 4, 2019 · 13 comments · May be fixed by #1686
Closed

[bug/proposal] for boolean prop type and enabled enableImplicitConversion flag #306

ruscon opened this issue Nov 4, 2019 · 13 comments · May be fixed by #1686
Labels
status: duplicate Issue is being tracked already in another issue. type: feature Issues related to new features.

Comments

@ruscon
Copy link
Contributor

ruscon commented Nov 4, 2019

version: "class-transformer": "0.2.3"

example:

import 'reflect-metadata';
import { Expose, plainToClass } from 'class-transformer';

class TestClass {
    @Expose()
    prop: boolean;
}

console.log(plainToClass(TestClass, {
    prop: 'false',
}, {
    enableImplicitConversion: true,
}));

returned: TestClass { prop: true }
expected: TestClass { prop: false }

proposal:
if value === 'true' || value === true || value === 1 || value === '1' then true
otherwise false

somethink like ths:

import { Transform } from 'class-transformer';

export function ToBoolean(): (target: any, key: string) => void {
    return Transform((value: any) => value === 'true' || value === true || value === 1 || value === '1');
}
@joeloudjinz
Copy link

Looks like we have the same issue.
see my SOF question

@dsbert
Copy link

dsbert commented Jan 7, 2020

This issue also means @IsBoolean is useless since any value can be transformed to a boolean.

@adzamkomladev
Copy link

Since this is still open, is there any work around?

@ruscon
Copy link
Contributor Author

ruscon commented Jan 9, 2020

@adzamkomladev I'm still waiting for an answer from the developer on how it should work

@leepood
Copy link

leepood commented Mar 11, 2020

same problem here

@mostafa-hz
Copy link

This issue also means @IsBoolean is useless since any value can be transformed to a boolean.

Until the issue gets fixed, I'm using this approach to handle it:

// ...

@IsBoolean()
@Transform(it => {
  switch (it) {
    case 'true':
      return true;
    case 'false':
      return false;
    default:
      return it;
  }
})
verified!: boolean;

// ...

@karam94
Copy link

karam94 commented Apr 10, 2020

Same problem here... I have a DTO with a boolean type on it, but I can't actually treat it like a boolean because at runtime it has type string everywhere...

@numToStr
Copy link

numToStr commented Apr 15, 2020

The shortest version I can think of

import { Transform } from "class-transformer";

export function ToBoolean() {
    return Transform(v => ["1", 1, "true", true].includes(v));
}

@DanielLoyAugmedix
Copy link

@numToStr I think this is rather misguided as it will convert all objects not in the set ["1", 1, "true", true] to false. I think this is far too broad a definition for type coercion.

@typestack typestack deleted a comment from pichillilorenzo Jul 29, 2020
@NoNameProvided
Copy link
Member

Hi all!

You can see some comments about this on #334 and #363. In short Boolean('false') == true so the library behaves as expected. We still need some discussion about the correct approach, most probably we will add a custom flag for this.

@NoNameProvided NoNameProvided added the status: has PR Issues which has a related PR closing the issue. label Jul 29, 2020
@davidoween
Copy link

I have same problem :(

any solution?

@NoNameProvided NoNameProvided added type: feature Issues related to new features. status: duplicate Issue is being tracked already in another issue. and removed status: has PR Issues which has a related PR closing the issue. labels Jan 14, 2021
@NoNameProvided
Copy link
Member

A tracking issue has been created for this at #550. If you have any feedback regarding the feature, please leave it there.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: duplicate Issue is being tracked already in another issue. type: feature Issues related to new features.
Development

Successfully merging a pull request may close this issue.