Skip to content

Commit

Permalink
feat(document): run setters on defaults
Browse files Browse the repository at this point in the history
Fix #8012
  • Loading branch information
vkarpov15 committed Jul 30, 2019
1 parent 9db4857 commit 39bebda
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/schematype.js
Expand Up @@ -892,7 +892,7 @@ SchemaType.prototype.getDefault = function(scope, init) {
ret = utils.clone(ret);
}

const casted = this.cast(ret, scope, init);
const casted = this.applySetters(ret, scope, init);
if (casted && casted.$isSingleNested) {
casted.$parent = scope;
}
Expand Down
5 changes: 4 additions & 1 deletion test/model.test.js
Expand Up @@ -4149,19 +4149,22 @@ describe('Model', function() {
});

it('setters trigger on null values (gh-1445)', function(done) {
const calls = [];
const OrderSchema = new Schema({
total: {
type: Number,
default: 0,
set: function(value) {
assert.strictEqual(null, value);
calls.push(value);
return 10;
}
}
});

const Order = db.model('order' + random(), OrderSchema);
const o = new Order({total: null});

assert.deepEqual(calls, [0, null]);
assert.equal(o.total, 10);
done();
});
Expand Down

0 comments on commit 39bebda

Please sign in to comment.