From 2f8152172761dfa0464bfda82c6774148763f1f5 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 13 Jun 2020 17:09:16 -0400 Subject: [PATCH] feat(SingleNestedPath+DocumentArray): add static `set()` function for global options, support setting `_id` globally Fix #8883 --- lib/schema.js | 6 ++++++ lib/schema/SingleNestedPath.js | 18 ++++++++++++++++++ lib/schema/documentarray.js | 18 ++++++++++++++++++ test/schema.documentarray.test.js | 16 ++++++++++++++++ test/schema.singlenestedpath.test.js | 19 +++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/lib/schema.js b/lib/schema.js index 1724dd08be9..cc721b074ee 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -936,6 +936,12 @@ Schema.prototype.interpretAsType = function(path, obj, options) { if (options.hasOwnProperty('typePojoToMixed')) { childSchemaOptions.typePojoToMixed = options.typePojoToMixed; } + if (this._userProvidedOptions.hasOwnProperty('_id')) { + childSchemaOptions._id = this._userProvidedOptions._id; + } else if (Schema.Types.DocumentArray.defaultOptions._id != null) { + childSchemaOptions._id = Schema.Types.DocumentArray.defaultOptions._id; + } + const childSchema = new Schema(cast, childSchemaOptions); childSchema.$implicitlyCreated = true; return new MongooseTypes.DocumentArray(path, childSchema, obj); diff --git a/lib/schema/SingleNestedPath.js b/lib/schema/SingleNestedPath.js index d7abd6cea77..dc7f11ec5f8 100644 --- a/lib/schema/SingleNestedPath.js +++ b/lib/schema/SingleNestedPath.js @@ -300,6 +300,24 @@ SingleNestedPath.prototype.discriminator = function(name, schema, value) { return this.caster.discriminators[name]; }; +/** + * Sets a default option for all SingleNestedPath instances. + * + * ####Example: + * + * // Make all numbers have option `min` equal to 0. + * mongoose.Schema.Embedded.set('required', true); + * + * @param {String} option - The option you'd like to set the value for + * @param {*} value - value for option + * @return {undefined} + * @function set + * @static + * @api public + */ + +SingleNestedPath.set = SchemaType.set; + /*! * ignore */ diff --git a/lib/schema/documentarray.js b/lib/schema/documentarray.js index 57e2fa441b0..c263fd66255 100644 --- a/lib/schema/documentarray.js +++ b/lib/schema/documentarray.js @@ -510,6 +510,24 @@ function scopePaths(array, fields, init) { return hasKeys && selected || undefined; } +/** + * Sets a default option for all DocumentArray instances. + * + * ####Example: + * + * // Make all numbers have option `min` equal to 0. + * mongoose.Schema.DocumentArray.set('_id', false); + * + * @param {String} option - The option you'd like to set the value for + * @param {*} value - value for option + * @return {undefined} + * @function set + * @static + * @api public + */ + +DocumentArrayPath.set = SchemaType.set; + /*! * Module exports. */ diff --git a/test/schema.documentarray.test.js b/test/schema.documentarray.test.js index 983bf9634de..70d35378510 100644 --- a/test/schema.documentarray.test.js +++ b/test/schema.documentarray.test.js @@ -98,4 +98,20 @@ describe('schema.documentarray', function() { assert.equal(doc.nested[0].length, 3); assert.equal(doc.nested[0][1].title, 'second'); }); + + it('supports `set()` (gh-8883)', function() { + mongoose.deleteModel(/Test/); + mongoose.Schema.Types.DocumentArray.set('_id', false); + + const Model = mongoose.model('Test', mongoose.Schema({ + arr: { type: [{ name: String }] } + })); + + const doc = new Model({ arr: [{ name: 'test' }] }); + + assert.equal(doc.arr.length, 1); + assert.ok(!doc.arr[0]._id); + + mongoose.Schema.Types.DocumentArray.set('_id', true); + }); }); diff --git a/test/schema.singlenestedpath.test.js b/test/schema.singlenestedpath.test.js index 5365776f587..80f55965ed7 100644 --- a/test/schema.singlenestedpath.test.js +++ b/test/schema.singlenestedpath.test.js @@ -143,4 +143,23 @@ describe('SingleNestedPath', function() { }); }); }); + + it('supports `set()` (gh-8883)', function() { + mongoose.deleteModel(/Test/); + mongoose.Schema.Types.Embedded.set('required', true); + + const Model = mongoose.model('Test', mongoose.Schema({ + nested: mongoose.Schema({ + test: String + }) + })); + + const doc = new Model({}); + + const err = doc.validateSync(); + assert.ok(err); + assert.ok(err.errors['nested']); + + mongoose.Schema.Types.Embedded.set('required', false); + }); }); \ No newline at end of file