Skip to content

Commit a6e4a36

Browse files
committed
fix: removed doc property from validation errors as per maintainer feedback
1 parent f49c751 commit a6e4a36

File tree

3 files changed

+4
-19
lines changed

3 files changed

+4
-19
lines changed

lib/model.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,9 +3042,6 @@ Model.$__insertMany = function(arr, options, callback) {
30423042
const results = ordered ? null : new Array(arr.length);
30433043
const toExecute = arr.map((doc, index) =>
30443044
callback => {
3045-
// Store original document for error reporting
3046-
const originalDoc = arr[index];
3047-
30483045
// If option `lean` is set to true bypass validation and hydration
30493046
if (lean) {
30503047
// we have to execute callback at the nextTick to be compatible
@@ -3079,9 +3076,8 @@ Model.$__insertMany = function(arr, options, callback) {
30793076
() => { callback(null, doc); },
30803077
error => {
30813078
if (ordered === false) {
3082-
// Add index and doc to validation error for better debugging
3079+
// Add index to validation error so users can identify which document failed
30833080
error.index = index;
3084-
error.doc = doc.toObject ? doc.toObject() : originalDoc;
30853081
validationErrors.push(error);
30863082
validationErrorsToOriginalOrder.set(error, index);
30873083
results[index] = error;

test/model.insertMany.test.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ describe('insertMany()', function() {
429429
assert.ok(!err.mongoose.validationErrors[1].errors['name']);
430430
});
431431

432-
it('insertMany() validation errors include index and doc properties with ordered false and rawResult', async function() {
432+
it('insertMany() validation errors include index property with ordered false and rawResult', async function() {
433433
const schema = new Schema({
434434
title: { type: String, required: true },
435435
requiredField: { type: String, required: true }
@@ -451,19 +451,15 @@ describe('insertMany()', function() {
451451
// Check first validation error (index 1)
452452
const error1 = res.mongoose.validationErrors[0];
453453
assert.equal(error1.index, 1);
454-
assert.ok(error1.doc);
455-
assert.equal(error1.doc.title, 'title2');
456454
assert.ok(error1.errors['requiredField']);
457455

458456
// Check second validation error (index 2)
459457
const error2 = res.mongoose.validationErrors[1];
460458
assert.equal(error2.index, 2);
461-
assert.ok(error2.doc);
462-
assert.equal(error2.doc.requiredField, 'field3');
463459
assert.ok(error2.errors['title']);
464460
});
465461

466-
it('insertMany() validation errors include index and doc properties with throwOnValidationError', async function() {
462+
it('insertMany() validation errors include index property with throwOnValidationError', async function() {
467463
const schema = new Schema({
468464
title: { type: String, required: true },
469465
requiredField: { type: String, required: true }
@@ -485,15 +481,11 @@ describe('insertMany()', function() {
485481
// Check first validation error (index 1)
486482
const error1 = err.validationErrors[0];
487483
assert.equal(error1.index, 1);
488-
assert.ok(error1.doc);
489-
assert.equal(error1.doc.title, 'title2');
490484
assert.ok(error1.errors['requiredField']);
491485

492486
// Check second validation error (index 2)
493487
const error2 = err.validationErrors[1];
494488
assert.equal(error2.index, 2);
495-
assert.ok(error2.doc);
496-
assert.equal(error2.doc.requiredField, 'field3');
497489
assert.ok(error2.errors['title']);
498490
});
499491

types/error.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ declare module 'mongoose' {
9090

9191
errors: { [path: string]: ValidatorError | CastError };
9292
addError: (path: string, error: ValidatorError | CastError) => void;
93-
93+
9494
/** Index of the document in insertMany() that failed validation (only set for unordered insertMany) */
9595
index?: number;
96-
97-
/** Document that failed validation (only set for unordered insertMany) */
98-
doc?: any;
9996

10097
constructor(instance?: MongooseError);
10198
}

0 commit comments

Comments
 (0)