Closed littlefyr closed 1 month ago
This is with version 0.5.1
I should add that I've also tested with @Type(() => URL)
and get the same results
I just realized that this is related to the question in #1624
Its worth noting that in this specific case, the following does work:
class TypeTransformClass {
id: number;
@Type(() => String)
@Transform(({ value }) => {
return value instanceof URL ? value : new URL(value);
})
image: URL;
}
But its not entirely obvious, or generic, approach
Hi,
In your inputObject
you pass in a URL
instance and class-transformer will call 'new Url()' on it because it infers the class from the plain object you passed in, but this happens before @Transform
is called but after the @Type
is called so that is why your code does not work correctly without @Type
applied.
I think this might not be a correct behavior, because it should not create a new instance from the type it received from the plainObject, but copy it. (or give some way to control this behavior).
Currently I think there are two workarounds:
@Type(() => String)
before the custom transformerplainToClass/plainToInstance
to call toString on URL instances in the plain object)Closing in favor of https://github.com/typestack/class-transformer/issues/1443
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.
Description
I have some classes that are defined to have URL values. When I attempt to use
plainToClass
in this situation,TypeError('Invalid URL')
gets thrown. This is true even if I use@Transform
to return a proper URL object.Minimal code-snippet showcasing the problem
Expected behavior
The expected behaviour is a passing test.
Actual behavior
I think the test output speaks for itself:
The stack trace tells the problem, it is trying to do
new URL()
which isn't allowed for URLs.I suspect this problem will be true of any Class that does not allow an empty constructor.