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

Model.save() doesn't throw StrictModeError #8149

Closed
ouyuran opened this issue Sep 10, 2019 · 3 comments
Closed

Model.save() doesn't throw StrictModeError #8149

ouyuran opened this issue Sep 10, 2019 · 3 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@ouyuran
Copy link
Contributor

ouyuran commented Sep 10, 2019

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When I set strict to 'throw', it works good on create() and update(). While when I try to set a inexist property to a model and call save(), it doesn't throw a StrictModeError but just drop the property.

If the current behavior is a bug, please provide the steps to reproduce.

Test case:

test('save() an inexistent property', async function () {
    const schema = new mongoose.Schema({
        name: String,
        year_of_birth: { type: Number, immutable: true }
    }, { strict: 'throw' });
    const Person = mongoose.model('Person', schema);
    const Joe = await Person.create({ name: 'Joe', year_of_birth: 2001 });
    const id = Joe.id;
    // Ok
    await assert.isRejected(Person.create({ name: 'Joe', inexistence: 'something' }));
    // Ok
    await assert.isRejected(Person.findByIdAndUpdate(id, { inexistence: 'something' }));
    // Doesn't throw error
    Joe.inexistence = 'something';
    await assert.isRejected(Joe.save());
})

What is the expected behavior?
It should throw a StrictModeError when using save().

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js 10.16.3
Mongoose 5.6.11
MongoDB 3.2.7

@ouyuran
Copy link
Contributor Author

ouyuran commented Sep 10, 2019

And it doesn't throw error when trying to modity an immutable property using save() either.

@ouyuran
Copy link
Contributor Author

ouyuran commented Sep 10, 2019

I tried set() and the result is quite confusing

  1. When I try to add a inexistent property using set(), it throws a StrictModeError, just as I expect
assert.throws(Joe.set.bind(Joe, {inexistence: 'something'}), StrictModeError);
  1. While on the other hand, when I try to modify a immutable property using set, it doesn't throw any error
Joe.set({ year_of_birth: 2002 });

@vkarpov15 vkarpov15 added this to the 5.7.1 milestone Sep 10, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Sep 10, 2019
@vkarpov15
Copy link
Collaborator

  1. Re: Joe.inexistence = 'something'; being fine, that's expected behavior. If you use = to set a path that isn't in your schema, Mongoose will never save that path, because Mongoose doesn't track changes on non-schema paths.

  2. Re: Joe.set({ year_of_birth: 2002 });, will be fixed in 5.7.1 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants