Class-transformer: [bug/proposal] for boolean prop type and enabled `enableImplicitConversion` flag

Created on 4 Nov 2019  路  13Comments  路  Source: typestack/class-transformer

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');
}
duplicate feature

Most helpful comment

The shortest version I can think of

import { Transform } from "class-transformer";

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

All 13 comments

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

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

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

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

same problem here

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;

// ...

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

The shortest version I can think of

import { Transform } from "class-transformer";

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

@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.

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.

I have same problem :(

any solution?

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braivre picture braivre  路  3Comments

ERPedersen picture ERPedersen  路  4Comments

leon19 picture leon19  路  5Comments

lazarljubenovic picture lazarljubenovic  路  6Comments

AckerApple picture AckerApple  路  5Comments