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');
}
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
@IsBooleanis 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.
Most helpful comment
The shortest version I can think of