Hi,
I don't know if this is a known issue since I could not find any.
Fact is, the plainToClass method assigns unknown properties to the class.
class User {
id: number
firstName: string
lastName: string
password: string
}
const fromPlainUser = {
unkownProp: 'hello there',
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman'
}
console.log(plainToClass(User, fromPlainUser))
Result is :
User {
unkownProp: 'hello there',
firstName: 'Umed',
lastName: 'Khudoiberdiev',
password: 'imnosuperman'
}
The only workaround that I saw looking at the doc would be to add an @Exclude decorator on the class and an @Expose decorator on each property, which is not really convenient...
Any other workaround ? Is this a known thing ?
Yes, it is known and it will be solved in the same way as in the class-validator lib. You will be able to provide an extra parameter to strip unknown properties.
I don't understand, this is the class-transformer lib 馃
Sorry, updated my comment, I meant class-validator.
Got it 馃槈
I somehow inferred something like that.
Should I do a PR for this then ?
@nimaen Please do :)
Any news about this ?
Sorry, I somehow forgot about it 馃懠
I'll try to have a look in the week.
Ok, so, I've digged down into the reflective api and what we can do with it, and it seems like, per nature, one cannot list a class properties from the constructor metadata without adding a decorator on each class property.
We could also list properties names only with Object.getOwnPropertyNames but it would require to have default values on each property, which is not good enough and does not cover all cases.
So, I'm back at my first comment again : the only way to do it _in a proper way_ and without adding new requirements would be to use the existing @Expose decorator, and to add a new option to only assign exposed properties, as @NoNameProvided suggested here.
Would that still be ok ?
Yes, i'm agree with @NoNameProvided comment :smile:
Well, I'm waiting for a response from the core team now :)
@nimaen I'm looking forward to your PR.
Well, there won't be any magic.
It would be only usable like this:
class User {
@Expose() id: number;
@Expose() firstName: string;
@Expose() lastName: string;
}
const plainUser = {
firstName: "Umed",
lastName: "Khudoiberdiev",
age: 12
};
plainToClass(User, plainUser, { excludeExtraneousValues: true })
And this would log :
User (
id: undefined,
firstName: "Umed",
lastName: "Khudoiberdiev"
)
@nimaen Thanks!
I have looked at your previous comment. Seems like there is no approach to adapt with expected behavior in your issue description with current package API (I also checked the source code as well).
excludeExtraneousValues is not yet included in an npm version :/
Could a collaborator publish a new version please ?
@NoNameProvided maybe ?
@AmirTugi I'm sorry but I cannot create a new Version in npm. @NoNameProvided should be able to so this
This is desirable. Waiting for it to be available in npm version...
Yeah i know . I'll try to talk to @NoNameProvided to talk about the right permissions
Please release a new version. This option would be so fucking important...
@MTschannett @NoNameProvided Hey, can we get the latest changes in NPM please, thanks!
I faced same problem! :sob:
Please release a new version.
You could use {whitelist: true} on the class-validator instead of excludeExtraneousValues in class-transformer
In the same position here. I user plain class-transformer to convert my DTO objects, and I really need the excludeExtraneousValues. Anyone that is available to update it on npm?
@pablosproject it's not available in npm. I've tried to build it, but without any success.
But if you use https://github.com/typestack/class-validator for your DTOs, you could set flag whitelist to true, it will do the same as excludeExtraneousValues for class-transformer.
For example for nest.js:
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
}))
Still waiting for new version with 'excludeExtraneousValues' option included.
Guys, if you really need this and use this lib as standalone package, you may use class-transformer-up which is fork of this develop branch published to npm, which supports above feature.
Hello! Why is this issue closed?
Because I've fixed it in a #222
@nimaen could you also publish it to npm? I think that is the biggest concern in this issue.
Nah, I'm a mere OSS contributor, I'm not a Typestack member.
waiting....
It's released in 0.2.1.
For people who are using this in combination with class-validator and thus are decorating the objects with @IsString() for example anyway.
I wrote a little extension that uses those decorators to strip all unknown properties from the object.
https://gist.github.com/ljobse/a617ca62e7d0277a8da351479656f04f
The file just needs to be loaded and the plainToClass function will now strip all properties which are not decorated with anything.
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
Please release a new version. This option would be so fucking important...