diff --git a/lib/index.js b/lib/index.js index 25adbea94d0..0cd7c1a468f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -440,15 +440,23 @@ Mongoose.prototype.modelNames.$hasSideEffects = true; /** * Generates a new Schema merged from others * + * @param {Object} [schema] Object with fields that the merged Schemas does not have * @param {Schema|Array} schemas * @param {Object} options options like in Schema constructor - * @param {Boolean} control says when the generated schema will have control over merged schemas + * @param {Boolean} [control] says when the generated schema will have control over merged schemas * @api public * @return {Schema} */ -Mongoose.prototype.mergeSchemas = function(schemas, options, control) -{ - var schema = {}, dstSchema, dict = {}; +Mongoose.prototype.mergeSchemas = function(schema, schemas, options, control) { + if (schema.constructor !== Object) { + control = options; + options = schemas; + schemas = schema; + schema = undefined; + } + schema = schema || {}; + + var dstSchema, dict = {}; var _kinds = [], _kind; var ret; // return object @@ -468,14 +476,15 @@ Mongoose.prototype.mergeSchemas = function(schemas, options, control) schemas.forEach(function(obj) { if (!(obj instanceof Schema)) throw new Error('Only Schema objects are allowed.'); + if (control && !obj.options.name) throw new Error('All SchemaS must have the name option defined.'); _kinds.push(obj.options.name); obj.eachPath(function(p) { - // ignored fields - if (obj.options._id && p.path === obj.options.typeKey) + // ignored fields, for now (_id) + if (obj.options._id && p === '_id') return; p = obj.path(p);