Skip to content

Commit

Permalink
Merge pull request #14769 from Automattic/vkarpov15/gh-6691-2
Browse files Browse the repository at this point in the history
fix: allow setting document array default to `null`
  • Loading branch information
vkarpov15 authored Jul 29, 2024
2 parents f921c9a + 9a2462c commit e6918a4
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 e6918a4

Please sign in to comment.