Skip to content

Commit

Permalink
fix: correct writeError index when insertMany() with ordered: false, …
Browse files Browse the repository at this point in the history
…rawResult: true with mixed validation error and write error re: #12791
  • Loading branch information
vkarpov15 committed Jan 3, 2023
1 parent a1f7f0a commit 9303f7d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/model.js
Expand Up @@ -3452,10 +3452,20 @@ Model.$__insertMany = function(arr, options, callback) {
callback(error, null);
return;
}

const originalDocIndex = new Map();
const validDocIndexToOriginalIndex = new Map();
for (let i = 0; i < docs.length; ++i) {
originalDocIndex.set(docs[i], i);
}

// We filter all failed pre-validations by removing nulls
const docAttributes = docs.filter(function(doc) {
return doc != null;
});
for (let i = 0; i < docAttributes.length; ++i) {
validDocIndexToOriginalIndex.set(i, originalDocIndex.get(docAttributes[i]));
}

// Make sure validation errors are in the same order as the
// original documents, so if both doc1 and doc2 both fail validation,
Expand Down Expand Up @@ -3506,6 +3516,13 @@ Model.$__insertMany = function(arr, options, callback) {
// `insertedDocs` is a Mongoose-specific property
const erroredIndexes = new Set((error && error.writeErrors || []).map(err => err.index));

for (let i = 0; i < error.writeErrors.length; ++i) {
error.writeErrors[i] = {
...error.writeErrors[i],
index: validDocIndexToOriginalIndex.get(error.writeErrors[i].index)
};
}

let firstErroredIndex = -1;
error.insertedDocs = docAttributes.
filter((doc, i) => {
Expand All @@ -3529,6 +3546,12 @@ Model.$__insertMany = function(arr, options, callback) {
return doc;
});

if (rawResult && ordered === false) {
error.mongoose = {
validationErrors: validationErrors
};
}

callback(error, null);
return;
}
Expand Down
29 changes: 29 additions & 0 deletions test/model.test.js
Expand Up @@ -4823,6 +4823,35 @@ describe('Model', function() {
assert.ok(!res.mongoose.validationErrors[1].errors['name']);
});

it('insertMany() validation error with ordered false and rawResult for mixed write and validation error (gh-12791)', async function() {
const schema = new Schema({
name: { type: String, required: true, unique: true },
year: { type: Number, required: true }
});
const Movie = db.model('Movie', schema);
await Movie.init();

const arr = [
{ foo: 'The Phantom Menace', year: 1999 },
{ name: 'The Force Awakens', bar: 2015 },
{ name: 'The Empire Strikes Back', year: 1980 },
{ name: 'The Empire Strikes Back', year: 1980 }
];
const opts = { ordered: false, rawResult: true };
const err = await Movie.insertMany(arr, opts).then(() => null, err => err);

assert.ok(err);
assert.equal(err.insertedDocs.length, 1);
assert.equal(err.insertedDocs[0].name, 'The Empire Strikes Back');
assert.equal(err.writeErrors.length, 1);
assert.equal(err.writeErrors[0].index, 3);
assert.equal(err.mongoose.validationErrors.length, 2);
assert.ok(err.mongoose.validationErrors[0].errors['name']);
assert.ok(!err.mongoose.validationErrors[0].errors['year']);
assert.ok(err.mongoose.validationErrors[1].errors['year']);
assert.ok(!err.mongoose.validationErrors[1].errors['name']);
});

it('insertMany() populate option (gh-9720)', async function() {
const schema = new Schema({
name: { type: String, required: true }
Expand Down

0 comments on commit 9303f7d

Please sign in to comment.