diff --git a/lib/options/SchemaArrayOptions.js b/lib/options/SchemaArrayOptions.js index 74c39f69098..ddd0b37a935 100644 --- a/lib/options/SchemaArrayOptions.js +++ b/lib/options/SchemaArrayOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaArrayOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * If this is an array of strings, an array of allowed values for this path. diff --git a/lib/options/SchemaBufferOptions.js b/lib/options/SchemaBufferOptions.js index 96577b989e2..258130dfee8 100644 --- a/lib/options/SchemaBufferOptions.js +++ b/lib/options/SchemaBufferOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaBufferOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * Set the default subtype for this buffer. diff --git a/lib/options/SchemaDateOptions.js b/lib/options/SchemaDateOptions.js index 5a88a77a8c2..09bf27f6adf 100644 --- a/lib/options/SchemaDateOptions.js +++ b/lib/options/SchemaDateOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaDateOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * If set, Mongoose adds a validator that checks that this path is after the diff --git a/lib/options/SchemaNumberOptions.js b/lib/options/SchemaNumberOptions.js index 6260ea74a10..e14e1cc9171 100644 --- a/lib/options/SchemaNumberOptions.js +++ b/lib/options/SchemaNumberOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaNumberOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * If set, Mongoose adds a validator that checks that this path is at least the diff --git a/lib/options/SchemaObjectIdOptions.js b/lib/options/SchemaObjectIdOptions.js index 440701cea31..cf887d0b7fe 100644 --- a/lib/options/SchemaObjectIdOptions.js +++ b/lib/options/SchemaObjectIdOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaObjectIdOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * If truthy, uses Mongoose's default built-in ObjectId path. diff --git a/lib/options/SchemaStringOptions.js b/lib/options/SchemaStringOptions.js index 8c6bb0ac067..765499355d6 100644 --- a/lib/options/SchemaStringOptions.js +++ b/lib/options/SchemaStringOptions.js @@ -17,12 +17,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions'); class SchemaStringOptions extends SchemaTypeOptions {} -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * Array of allowed values for this path diff --git a/lib/options/SchemaTypeOptions.js b/lib/options/SchemaTypeOptions.js index eef74f6829f..0a7236db8fe 100644 --- a/lib/options/SchemaTypeOptions.js +++ b/lib/options/SchemaTypeOptions.js @@ -23,12 +23,7 @@ class SchemaTypeOptions { } } -const opts = { - enumerable: true, - configurable: true, - writable: true, - value: null -}; +const opts = require('./propertyOptions'); /** * The type to cast this path to. diff --git a/lib/options/propertyOptions.js b/lib/options/propertyOptions.js new file mode 100644 index 00000000000..b7488a37ee9 --- /dev/null +++ b/lib/options/propertyOptions.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = Object.freeze({ + enumerable: true, + configurable: true, + writable: true, + value: void 0 +}); \ No newline at end of file diff --git a/lib/schema.js b/lib/schema.js index 00acac49b2f..73318da0859 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -7,6 +7,7 @@ const EventEmitter = require('events').EventEmitter; const Kareem = require('kareem'); const SchemaType = require('./schematype'); +const SchemaTypeOptions = require('./options/SchemaTypeOptions'); const VirtualType = require('./virtualtype'); const applyTimestampsToChildren = require('./helpers/update/applyTimestampsToChildren'); const applyTimestampsToUpdate = require('./helpers/update/applyTimestampsToUpdate'); @@ -426,7 +427,7 @@ Schema.prototype.add = function add(obj, prefix) { '`, got value "' + obj[key][0] + '"'); } - if (utils.isPOJO(obj[key]) && + if ((utils.isPOJO(obj[key]) || obj[key] instanceof SchemaTypeOptions) && (!obj[key][this.options.typeKey] || (this.options.typeKey === 'type' && obj[key].type.type))) { if (Object.keys(obj[key]).length) { // nested object { last: { name: String }} @@ -795,7 +796,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) { // copy of SchemaTypes re: gh-7158 gh-6933 const MongooseTypes = this.base != null ? this.base.Schema.Types : Schema.Types; - if (obj.constructor) { + if (!utils.isPOJO(obj) && !(obj instanceof SchemaTypeOptions)) { const constructorName = utils.getFunctionName(obj.constructor); if (constructorName !== 'Object') { const oldObj = obj;