diff --git a/lib/document.js b/lib/document.js index 6c4b665e56c..e4c71b3327f 100644 --- a/lib/document.js +++ b/lib/document.js @@ -79,6 +79,7 @@ function Document(obj, fields, skipId, options) { options = skipId; skipId = options.skipId; } + options = Object.assign({}, options); // Support `browserDocument.js` syntax if (this.$__schema == null) { @@ -92,9 +93,9 @@ function Document(obj, fields, skipId, options) { } this.$__ = new InternalCache(); - this.$isNew = options && options.isNew != null ? options.isNew : true; + this.$isNew = 'isNew' in options ? options.isNew : true; - if (options && options.priorDoc != null) { + if (options.priorDoc != null) { this.$__.priorDoc = options.priorDoc; } @@ -107,20 +108,18 @@ function Document(obj, fields, skipId, options) { } let defaults = true; - if (options && options.defaults !== undefined) { + if (options.defaults !== undefined) { this.$__.defaults = options.defaults; defaults = options.defaults; } const schema = this.$__schema; - let strict; if (typeof fields === 'boolean' || fields === 'throw') { this.$__.strictMode = fields; - strict = fields; fields = undefined; } else { - strict = schema.options.strict; + this.$__.strictMode = schema.options.strict; if (fields != null) { this.$__.selected = fields; } @@ -170,15 +169,15 @@ function Document(obj, fields, skipId, options) { // Function defaults get applied **after** setting initial values so they // see the full doc rather than an empty one, unless they opt out. // Re: gh-3781, gh-6155 - if (options && options.willInit && defaults) { + if (options.willInit && defaults) { if (options.skipDefaults) { this.$__.skipDefaults = options.skipDefaults; } } else if (defaults) { - $__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options && options.skipDefaults); + $__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults); } - if (!strict && obj) { + if (!this.$__.strictMode && obj) { const _this = this; const keys = Object.keys(this._doc); diff --git a/lib/helpers/schematype/handleImmutable.js b/lib/helpers/schematype/handleImmutable.js index 4a7453958e0..cc22c914922 100644 --- a/lib/helpers/schematype/handleImmutable.js +++ b/lib/helpers/schematype/handleImmutable.js @@ -40,8 +40,7 @@ function createImmutableSetter(path, immutable) { const _value = this.$__.priorDoc != null ? this.$__.priorDoc.$__getValue(path) : this.$__getValue(path); - const strict = this.$__.strictMode == null ? this.$__schema.options.strict : this.$__.strictMode; - if (strict === 'throw' && v !== _value) { + if (this.$__.strictMode === 'throw' && v !== _value) { throw new StrictModeError(path, 'Path `' + path + '` is immutable ' + 'and strict mode is set to throw.', true); } diff --git a/lib/schema/SubdocumentPath.js b/lib/schema/SubdocumentPath.js index b3f6442af6d..94a8c588628 100644 --- a/lib/schema/SubdocumentPath.js +++ b/lib/schema/SubdocumentPath.js @@ -167,7 +167,7 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) { } return obj; }, null); - options = priorVal != null ? Object.assign({}, options, { priorDoc: priorVal }) : options; + options = Object.assign({}, options, { priorDoc: priorVal }); if (init) { subdoc = new Constructor(void 0, selected, doc); subdoc.$init(val); diff --git a/lib/schema/documentarray.js b/lib/schema/documentarray.js index 640d0420060..d69a34af37d 100644 --- a/lib/schema/documentarray.js +++ b/lib/schema/documentarray.js @@ -391,6 +391,8 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) { let selected; let subdoc; + options = options || {}; + if (!Array.isArray(value)) { if (!init && !DocumentArrayPath.options.castNonArrays) { throw new CastError('DocumentArray', value, this.path, null, this); @@ -405,7 +407,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) { // We need to create a new array, otherwise change tracking will // update the old doc (gh-4449) - if (!options || !options.skipDocumentArrayCast || utils.isMongooseDocumentArray(value)) { + if (!options.skipDocumentArrayCast || utils.isMongooseDocumentArray(value)) { value = new MongooseDocumentArray(value, this.path, doc); } @@ -413,7 +415,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) { value[arrayAtomicsSymbol] = prev[arrayAtomicsSymbol] || {}; } - if (options && options.arrayPathIndex != null) { + if (options.arrayPathIndex != null) { value[arrayPathSymbol] = this.path + '.' + options.arrayPathIndex; } diff --git a/lib/schematype.js b/lib/schematype.js index b868c82de0f..53a687370d4 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -1181,7 +1181,6 @@ SchemaType.prototype._castNullish = function _castNullish(v) { SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) { let v = this._applySetters(value, scope, init, priorVal, options); - if (v == null) { return this._castNullish(v); }