Skip to content

Commit

Permalink
fix(schema): throw more helpful error when using schema from a differ…
Browse files Browse the repository at this point in the history
…ent version of Mongoose module

Re: #10453
  • Loading branch information
vkarpov15 committed Mar 13, 2022
1 parent 50b670c commit ab97a17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ Connection.prototype.model = function(name, schema, collection, options) {
if (!schema.instanceOfSchema) {
schema = new Schema(schema);
} else if (!(schema instanceof this.base.Schema)) {
schema = this.base.Schema.prototype.clone.call(schema);
schema = schema._clone(this.base.Schema);
}
}
if (schema && !schema.instanceOfSchema) {
Expand Down
48 changes: 28 additions & 20 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,22 @@ Schema.prototype.tree;
*/

Schema.prototype.clone = function() {
const s = new this.constructor({}, this._userProvidedOptions);
const s = this._clone();

// Bubble up `init` for backwards compat
s.on('init', v => this.emit('init', v));

return s;
};

/*!
* ignore
*/

Schema.prototype._clone = function _clone(Constructor) {
Constructor = Constructor || (this.base == null ? Schema : this.base.Schema);

const s = new Constructor({}, this._userProvidedOptions);
s.base = this.base;
s.obj = this.obj;
s.options = utils.clone(this.options);
Expand Down Expand Up @@ -358,9 +373,6 @@ Schema.prototype.clone = function() {

s.aliases = Object.assign({}, this.aliases);

// Bubble up `init` for backwards compat
s.on('init', v => this.emit('init', v));

return s;
};

Expand Down Expand Up @@ -952,29 +964,25 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
// new Schema({ path: [new Schema({ ... })] })
if (cast && cast.instanceOfSchema) {
if (!(cast instanceof Schema)) {
try {
cast = Schema.prototype.clone.call(cast);
} catch (err) {
throw new TypeError('Schema for array path `' + path +
'` is from a different, incompatible copy of the Mongoose module. ' +
'Please make sure you\'re using the same version ' +
'of Mongoose everywhere with `npm list mongoose`.');
}
throw new TypeError('Schema for array path `' + path +
'` is from a different copy of the Mongoose module. ' +
'Please make sure you\'re using the same version ' +
'of Mongoose everywhere with `npm list mongoose`. If you are still ' +
'getting this error, please add `new Schema()` around the path: ' +
`${path}: new Schema(...)`);
}
return new MongooseTypes.DocumentArray(path, cast, obj);
}
if (cast &&
cast[options.typeKey] &&
cast[options.typeKey].instanceOfSchema) {
if (!(cast[options.typeKey] instanceof Schema)) {
try {
cast[options.typeKey] = Schema.prototype.clone.call(cast[options.typeKey]);
} catch (err) {
throw new TypeError('Schema for array path `' + path +
'` is from a different, incompatible copy of the Mongoose module. ' +
'Please make sure you\'re using the same version ' +
'of Mongoose everywhere with `npm list mongoose`.');
}
throw new TypeError('Schema for array path `' + path +
'` is from a different copy of the Mongoose module. ' +
'Please make sure you\'re using the same version ' +
'of Mongoose everywhere with `npm list mongoose`. If you are still ' +
'getting this error, please add `new Schema()` around the path: ' +
`${path}: new Schema(...)`);
}
return new MongooseTypes.DocumentArray(path, cast[options.typeKey], obj, cast);
}
Expand Down

0 comments on commit ab97a17

Please sign in to comment.