Skip to content

feature: allow casting of primitive values during transformation #550

Open
@NoNameProvided

Description

@NoNameProvided

Description

By design, class-transformer doesn't alter the value of properties when transforming an object into a class, but simply copies them over. However, there is an option (enableImplicitConversion) to change this behavior, when enabled the lib always attempts to cast the handled property value to the target property type. There are scenarios when some of the transformed properties should be casted in a non-standard way. The most common type of this is the casting stringified "true" and "false" to boolean. Currently, the library will cast both values to true, because Boolean("<any-non-zero-lenght-string>") will be always true.

There have been various discussions requesting to mark properties to be casted in a special way.

The most common scenario when this is required is using the @Query parameter in NestJS.

Proposed solution

There is two main way to approach this: adding decorators for each supported primitive type or enabling the feature at the transformation level via a new global transform option. Both have their pros and cons:

  • adding decorators makes it easier to customize the feature to the users need as they can choose to only transform specific properties with this new decorator
  • adding it as a global feature flag makes it easier to apply it at scale if someone needs this as the default behavior

Generally, I believe the decorator approach to be better and I propose the following decorators:

  • @CastToBoolean - transforms boolean, "true", "false", 0 and 1 to the appropriate boolean values
  • @CastToFloat - transforms a stringified number or number to the number representation, but not null or undefined
  • @CastToInteger - transforms a stringified number or number to an integer representation of the number, the number is rounded, but not null or undefined
  • @CastToString - transforms any received values to the string representation but not null or undefined
  • @CastToUndefined - transforms "undefined" to undefined
  • @CastToNull - transforms "null" to undefined

A single property can have multiple of these decorators applied.

Every one of these decorators accepts a single option:

@CastToBoolean({ force: boolean })

When set to true the decorator will always attempt to transform the received value (except when it's null or undefined). However, when it is false (the default value) it will only attempt to transform values when it makes sense. For example:

  • for booleans it will only transform "true", "false", 0, 1 and skip the transformation if any other value is received
  • for numbers it will only transform if the value can be parsed as a float number (aka: "not-number" will be skipped)

The transformation must take place before the implicit conversion of the value (if enabled). When enableImplicitConversion is enabled both transformations will run on the given property.

Please do not comment non-related stuff like +1 or waiting for this. If you want to express interest you can like the issue.

Metadata

Metadata

Labels

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