Skip to content

Commit

Permalink
fix(document): throw strict mode error if setting an immutable path w…
Browse files Browse the repository at this point in the history
…ith strict mode: false

Fix #8149
  • Loading branch information
vkarpov15 committed Sep 12, 2019
1 parent 74f558c commit 8d86802
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/document.js
Expand Up @@ -1111,8 +1111,12 @@ Document.prototype.$set = function $set(path, val, type, options) {

this.$markValid(path);
} catch (e) {
this.invalidate(path,
new MongooseError.CastError(schema.instance, val, path, e));
if (e instanceof MongooseError.StrictModeError) {
this.invalidate(path, e);
} else {
this.invalidate(path,
new MongooseError.CastError(schema.instance, val, path, e));
}
shouldSet = false;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/schematype/handleImmutable.js
@@ -1,5 +1,7 @@
'use strict';

const StrictModeError = require('../../error/strict');

/*!
* ignore
*/
Expand Down Expand Up @@ -31,6 +33,10 @@ function createImmutableSetter(path, immutable) {
if (!_immutable) {
return v;
}
if (this.$__.strictMode === 'throw' && v !== this[path]) {
throw new StrictModeError(path, 'Path `' + path + '` is immutable ' +
'and strict mode is set to throw.');
}

return this[path];
};
Expand Down

0 comments on commit 8d86802

Please sign in to comment.