@@ -429,6 +429,74 @@ 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 ( ) {
433+ const schema = new Schema ( {
434+ title : { type : String , required : true } ,
435+ requiredField : { type : String , required : true }
436+ } ) ;
437+ const User = db . model ( 'User' , schema ) ;
438+
439+ const arr = [
440+ { title : 'title1' , requiredField : 'field1' } ,
441+ { title : 'title2' } , // Missing requiredField
442+ { requiredField : 'field3' } , // Missing title
443+ { title : 'title4' , requiredField : 'field4' }
444+ ] ;
445+ const opts = { ordered : false , rawResult : true } ;
446+ const res = await User . insertMany ( arr , opts ) ;
447+
448+ assert . equal ( res . insertedCount , 2 ) ;
449+ assert . equal ( res . mongoose . validationErrors . length , 2 ) ;
450+
451+ // Check first validation error (index 1)
452+ const error1 = res . mongoose . validationErrors [ 0 ] ;
453+ assert . equal ( error1 . index , 1 ) ;
454+ assert . ok ( error1 . doc ) ;
455+ assert . equal ( error1 . doc . title , 'title2' ) ;
456+ assert . ok ( error1 . errors [ 'requiredField' ] ) ;
457+
458+ // Check second validation error (index 2)
459+ const error2 = res . mongoose . validationErrors [ 1 ] ;
460+ assert . equal ( error2 . index , 2 ) ;
461+ assert . ok ( error2 . doc ) ;
462+ assert . equal ( error2 . doc . requiredField , 'field3' ) ;
463+ assert . ok ( error2 . errors [ 'title' ] ) ;
464+ } ) ;
465+
466+ it ( 'insertMany() validation errors include index and doc properties with throwOnValidationError' , async function ( ) {
467+ const schema = new Schema ( {
468+ title : { type : String , required : true } ,
469+ requiredField : { type : String , required : true }
470+ } ) ;
471+ const User = db . model ( 'User' , schema ) ;
472+
473+ const arr = [
474+ { title : 'title1' , requiredField : 'field1' } ,
475+ { title : 'title2' } , // Missing requiredField
476+ { requiredField : 'field3' } , // Missing title
477+ { title : 'title4' , requiredField : 'field4' }
478+ ] ;
479+ const opts = { ordered : false , rawResult : true , throwOnValidationError : true } ;
480+ const err = await User . insertMany ( arr , opts ) . then ( ( ) => null , err => err ) ;
481+
482+ assert . ok ( err ) ;
483+ assert . equal ( err . validationErrors . length , 2 ) ;
484+
485+ // Check first validation error (index 1)
486+ const error1 = err . validationErrors [ 0 ] ;
487+ assert . equal ( error1 . index , 1 ) ;
488+ assert . ok ( error1 . doc ) ;
489+ assert . equal ( error1 . doc . title , 'title2' ) ;
490+ assert . ok ( error1 . errors [ 'requiredField' ] ) ;
491+
492+ // Check second validation error (index 2)
493+ const error2 = err . validationErrors [ 1 ] ;
494+ assert . equal ( error2 . index , 2 ) ;
495+ assert . ok ( error2 . doc ) ;
496+ assert . equal ( error2 . doc . requiredField , 'field3' ) ;
497+ assert . ok ( error2 . errors [ 'title' ] ) ;
498+ } ) ;
499+
432500 it ( 'insertMany() populate option (gh-9720)' , async function ( ) {
433501 const schema = new Schema ( {
434502 name : { type : String , required : true }
0 commit comments