-
Notifications
You must be signed in to change notification settings - Fork 839
Whitelisting properties #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@pleerock Have you reviewed this? Any thoughts? I would like use this feature in my project as soon as possible. Thanks |
|
Throwing error is not user-friendly, we should strip instead. |
|
@19majkel94 @NoNameProvided can you guys please review this one and let me know what do you think about this change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ok! However I don't find it useful, validation is kinda like interface based, so we want the properties to be valid. Additional properties should be stipped off by class-transformer, not flagged as validation error. @Allowed decorator introduce more decorator noise, together with @IsDefined and @IsOptional, which looks bad for me.
Yes as I wrote I think the same, can the OP modify the PR? ping @aljazerzen |
|
@19majkel94 So are you saying we should move implement this in class-transformer or just to change it so it stripes off additional properties? |
|
Guys, can you merge this PR? I totally agree that this job should be done by class-transformer but I've reviewed issues about that in the class-transformer project and it seems to be not possible to do. So in that situation, this PR makes sense for me. BTW. Without stripping off additional properties this whole library is useless on production... I don't want to rewrite input manually everytime when it comes to my app just to make sure that it doesn't contain any unwanted additional fields. |
|
Sure, will implement stripping. |
|
I'd be happy if someone could merge this PR as it is. If you take look at Joi liblary it alos throws exceptions when object contains unallowed properties. Due to fact that stipping properties it not so trivial it could be implemented in future. |
|
I have implemented stripping of the additional properties and added flag for throwing an error. (sorry it took me this long). |
|
@pleerock The only concern: this PR currently won't break any existing projects that except if they depend on non-defined properties (which they should't). But if I implement this idea it will. That's why I am asking you to confirm we want this behavior. PS: why do I even need this stripping? I am putting the whole object I get from request into MongoDB via Mongoose. If someone for example passes a property |
|
Refactored
@pleerock this is my final version - can you merge it and get it on npm? |
|
@NoNameProvided can you merge this PR? |
|
Hey @aljazerzen! This PR looks good to me, but I would like to wait for a review from @pleerock on this one. (I cannot publish to npm anyway, so we need @pleerock to publish it.) |
|
Hi @pleerock! Do you have an estimation for merging this PR and publish a new release to NPM with this new feature? Also thanks, for the amazing work @aljazerzen I was looking for such feature and I'm really glad you already implemented it. 👍 |
|
Hi @pterblgh! I am actively going through issues and prs for class-validators, when I have a full picture of the current state, open features and future vision I will start merging these prs and implement the fitting features for the library. You can expect this PR being merged/reworked in the next 1-2 week. |
|
@aljazerzen thanks for your work! 🎉 🎉 🎉 |
|
@NoNameProvided Hey! I am glad to see my code being approved! Today I have updated to version Should I rather create a new pull request? (it is my first time contributing so I am not quite sure) Thanks! |
|
Thanks, fixed and released as |
|
🥇 |
|
Correct me if I'm wrong, but as I understand it, the |
|
Yes it does, but only if you pass it |
|
Fair enough 🙂 I still think that, with this (great) feature, the |
I share your view, however creating a deep copy is expensive. This would need some benchmarking before implementation. The stripping of properties is turned off by default, so we mutate the object only if you opt in for it. |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
As discussed here I only solution to unwanted additional properties is to add a decorator to all wanted properties. I know this can become messy, but for now, it is better than nothing. If in the future Typescript will indeed set all properties to undefined, we can upgrade this without breaking any code written using
@Alloweddecorator.Because this is a validator-class
@Allowedis not stripping additional properties, but throwing a validation error. Should we add an option to strip instead?I have tried to use formatting style used in this repo, I hope I did a good job :)