Skip to content

Commit

Permalink
fix(schema): support Schema#add() with schematype instances with di…
Browse files Browse the repository at this point in the history
…fferent paths

Fix #9370
  • Loading branch information
vkarpov15 committed Aug 29, 2020
1 parent f0c53b8 commit 71abbe1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/schema.js
Expand Up @@ -875,7 +875,9 @@ Object.defineProperty(Schema.prototype, 'base', {

Schema.prototype.interpretAsType = function(path, obj, options) {
if (obj instanceof SchemaType) {
return obj;
const clone = obj.clone();
clone.path = path;
return clone;
}

// If this schema has an associated Mongoose object, use the Mongoose object's
Expand Down
24 changes: 24 additions & 0 deletions test/schema.test.js
Expand Up @@ -2471,4 +2471,28 @@ describe('schema', function() {

assert.equal(schema.path('arr').caster.instance, 'Mixed');
});

it('handles using a schematype when defining a path (gh-9370)', function() {
const schema1 = Schema({
foo: {
type: Number,
min: 4,
get: get
}
});

function get(v) {
return Math.floor(v);
}

const schema2 = Schema({
bar: schema1.path('foo')
});

const schematype = schema2.path('bar');
assert.equal(schematype.path, 'bar');
assert.equal(schematype.options.type, Number);
assert.equal(schematype.options.min, 4);
assert.equal(schematype.options.get, get);
});
});

0 comments on commit 71abbe1

Please sign in to comment.