Skip to content
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

fix(types): avoid adding non-existent properties from model constructor for typegoose #11986

Merged
merged 2 commits into from
Jun 25, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #11960

Please take a look @mohammad0-0ahmad

Summary

In #11563, it looks like the model constructor pulls types from the parameter to the model constructor, which is very wrong. @hasezoey reported that this also causes issues for Typegoose.

The tests in document.test.ts#L195-L196 are also wrong, unExistProperty shouldn't be defined on the document instance. See the below script:

const mongoose = require('mongoose');

void async function main() {
  await mongoose.connect('mongodb://localhost:27017');

  const Test = mongoose.model('Test', mongoose.Schema({ name: String }));
  const doc = new Test({ name: 'test', unExistProperty: 1 });
  console.log(doc);
  console.log(doc.name); // 'test'
  console.log(doc.unExistProperty); // undefined

  await mongoose.disconnect();
}();

Examples

@vkarpov15 vkarpov15 requested a review from Uzlopak June 24, 2022 20:53
@vkarpov15 vkarpov15 self-assigned this Jun 24, 2022
@mohammad0-0ahmad
Copy link
Contributor

mohammad0-0ahmad commented Jun 25, 2022

Hello @vkarpov15!
The changes look good to cover the regression.
Because of the requirements, you described "arbitrary objects in new and create and insert". I did these changes.
Anyway, I would like to say this changes make it better, and I would prefer as will to prevent adding "arbitrary objects" when strict mode is enabled.
I will consider making that available, "If you think that correct"
Thanks for fixing this regression @vkarpov15, and sorry for the trouble @hasezoey, and thanks for reporting the issue.
Best Regards!

@vkarpov15 vkarpov15 merged commit 4f89f23 into master Jun 25, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-11960 branch June 25, 2022 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Types] mongoose 6.4.0 (PR 11563) broke explicitly set property types
2 participants