Skip to content

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

@ouyuran

Description

@ouyuran

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions