Skip to content

feat: support automatic casting of the string representation of a boolean value #1328

Closed as not planned
@mubin-ct

Description

@mubin-ct

Description

If we try to convert a value that has typeof string, then in some cases it returns the wrong value. e.g value: 'true' will return true, while on the other hand value: 'false' will also return the true.

Minimal code-snippet showcasing the problem
Consideration: A network access request.

import { Expose, plainToInstance } from 'class-transformer';

class RoleDTO {
  @Expose()
    id: number
  @Expose()
  canAccess: boolean
}
plainToInstance(RoleDTO, { id: 1, canAccess: 'true' }, { enableImplicitConversion: true })
plainToInstance(RoleDTO, { id: 2, canAccess: 'false' }, { enableImplicitConversion: true })

Expected behavior

RoleDTO with the id:1 should have canAccess: true and RoleDTO with the id:2 should have canAccess: false

Actual behavior

RoleDTO with id 1, and 2 have canAccess: true

This is a common JS Boolean conversion issue, I will be submitting a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicateIssue is being tracked already in another issue.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions