Skip to content

Commit

Permalink
feat(SingleNestedPath+DocumentArray): add static set() function for…
Browse files Browse the repository at this point in the history
… global options, support setting `_id` globally

Fix #8883
  • Loading branch information
vkarpov15 committed Jun 13, 2020
1 parent 37de1fc commit 2f81521
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/schema.js
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions lib/schema/SingleNestedPath.js
Expand Up @@ -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
*/
Expand Down
18 changes: 18 additions & 0 deletions lib/schema/documentarray.js
Expand Up @@ -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.
*/
Expand Down
16 changes: 16 additions & 0 deletions test/schema.documentarray.test.js
Expand Up @@ -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);
});
});
19 changes: 19 additions & 0 deletions test/schema.singlenestedpath.test.js
Expand Up @@ -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);
});
});

0 comments on commit 2f81521

Please sign in to comment.