Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(document): run setters on defaults #8035

Merged
merged 1 commit into from Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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