Skip to content

Commit

Permalink
fix: allow setting document array default to null
Browse files Browse the repository at this point in the history
Fix #6691
Re: #14717
  • Loading branch information
vkarpov15 committed Jul 29, 2024
1 parent f921c9a commit 9a2462c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/schema/documentArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {

const fn = this.defaultValue;

if (!('defaultValue' in this) || fn !== void 0) {
if (!('defaultValue' in this) || fn != null) {
this.default(function() {
let arr = fn.call(this);
if (arr != null && !Array.isArray(arr)) {
Expand Down
6 changes: 6 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3203,16 +3203,22 @@ describe('document', function() {
names: {
type: [String],
default: null
},
tags: {
type: [{ tag: String }],
default: null
}
});

const Model = db.model('Test', schema);
const m = new Model();
assert.strictEqual(m.names, null);
assert.strictEqual(m.tags, null);
await m.save();

const doc = await Model.collection.findOne({ _id: m._id });
assert.strictEqual(doc.names, null);
assert.strictEqual(doc.tags, null);
});

it('validation works when setting array index (gh-3816)', async function() {
Expand Down

0 comments on commit 9a2462c

Please sign in to comment.