Skip to content

Commit d423b0e

Browse files
authored
Merge pull request #14128 from Automattic/vkarpov15/gh-14101
fix(schema): avoid creating unnecessary clone of schematype in nested array so nested document arrays use correct constructor
2 parents b9f29f0 + 4817c60 commit d423b0e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/schema.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,15 +1122,14 @@ Schema.prototype.path = function(path, obj) {
11221122
if (_schemaType.$isMongooseDocumentArray) {
11231123
_schemaType.$embeddedSchemaType._arrayPath = arrayPath;
11241124
_schemaType.$embeddedSchemaType._arrayParentPath = path;
1125-
_schemaType = _schemaType.$embeddedSchemaType.clone();
1125+
_schemaType = _schemaType.$embeddedSchemaType;
11261126
} else {
11271127
_schemaType.caster._arrayPath = arrayPath;
11281128
_schemaType.caster._arrayParentPath = path;
1129-
_schemaType = _schemaType.caster.clone();
1129+
_schemaType = _schemaType.caster;
11301130
}
11311131

1132-
_schemaType.path = arrayPath;
1133-
toAdd.push(_schemaType);
1132+
this.subpaths[arrayPath] = _schemaType;
11341133
}
11351134

11361135
for (const _schemaType of toAdd) {

test/types.documentarray.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,4 +754,28 @@ describe('types.documentarray', function() {
754754

755755
assert.equal(doc.myMap.get('foo').$path(), 'myMap.foo');
756756
});
757+
758+
it('bubbles up validation errors from doubly nested doc arrays (gh-14101)', async function() {
759+
const optionsSchema = new mongoose.Schema({
760+
val: {
761+
type: Number,
762+
required: true
763+
}
764+
});
765+
766+
const testSchema = new mongoose.Schema({
767+
name: String,
768+
options: {
769+
type: [[optionsSchema]],
770+
required: true
771+
}
772+
});
773+
774+
const Test = db.model('Test', testSchema);
775+
776+
await assert.rejects(
777+
Test.create({ name: 'test', options: [[{ val: null }]] }),
778+
/options.0.0.val: Path `val` is required./
779+
);
780+
});
757781
});

0 commit comments

Comments
 (0)