Class-transformer: Option to disable implicit type conversion introduced in 0.2.2

Created on 16 May 2019  路  8Comments  路  Source: typestack/class-transformer

In this commit an implicit type conversion based on TS reflected type was introduced. It makes DTO validation less strict without a way to disable it.
Consider us having a DTO "Cat":

import { IsString } from 'class-validator'

class CatDTO {
  @IsString()
  tail: string
}

If we convert a plan object with tail as a number to CatDTO using plainToClass, class-transformer would convert tail to a string automatically.

import { validate } from 'class-validator'

const obj = {
  tail: 123
}
const catInstance = plainToClass(CatDTO, obj)
// { "tail":"123" }
validate(catInstance) // passes

With this implicit type conversion we can no longer have a strict validation of incoming data.

This functionality could be useful to some people, but I believe it should be optional and disabled by default.

What do you think?

Would you be willing to accept a PR which adds a new option to plainToClass - implicitTypeConversion set to false?

fix

Most helpful comment

Hi all!

0.2.3 has been released what hides the implicit conversion behind the enableImplicitConversion flag and disabled by default. It also reverts a change what caused issues with the @Exclude decorator (#254).

Please let me know if the problem is fixed. Thanks.

All 8 comments

This change has broken many e2e tests in lots of NestJS production applications since it introduced a breaking change in the library behavior.

With this implicit type conversion we can no longer have a strict validation of incoming data.

This change broke my application. @NoNameProvided make this functionality optional, please. I had to decrease class-transformer version to keep strict validation working well.

Hi all! Thanks for the feedback! This will be addressed. Until it鈥檚 fixed you can pin class-transformer at 0.2.0 to prevent this behavior.

Thanks @NoNameProvided! I'll pin the version as soon as possible @vizlibdk

Hi all!

0.2.3 has been released what hides the implicit conversion behind the enableImplicitConversion flag and disabled by default. It also reverts a change what caused issues with the @Exclude decorator (#254).

Please let me know if the problem is fixed. Thanks.

Hi @NoNameProvided,

Thanks for the update! To me, upgrading from 0.2.0 to 0.2.3 worked like a charm.

Hi @NoNameProvided thank you very much, everything works fine!

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