Skip to content

Commit

Permalink
Merge pull request #8035 from Automattic/gh8012
Browse files Browse the repository at this point in the history
feat(document): run setters on defaults
  • Loading branch information
vkarpov15 authored Aug 31, 2019
2 parents 9cae665 + 39bebda commit 751ba48
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 751ba48

Please sign in to comment.