@@ -3946,6 +3946,46 @@ describe('document', function() {
39463946 catch ( done ) ;
39473947 } ) ;
39483948
3949+ it ( 'runs validate hooks on arrays subdocs if not directly modified (gh-5861)' , function ( done ) {
3950+ var childSchema = new Schema ( {
3951+ name : { type : String } ,
3952+ friends : [ { type : String } ]
3953+ } ) ;
3954+ var count = 0 ;
3955+
3956+ childSchema . pre ( 'validate' , function ( next ) {
3957+ ++ count ;
3958+ next ( ) ;
3959+ } ) ;
3960+
3961+ var parentSchema = new Schema ( {
3962+ name : { type : String } ,
3963+ children : [ childSchema ]
3964+ } ) ;
3965+
3966+ var Parent = db . model ( 'gh5861' , parentSchema ) ;
3967+
3968+ var p = new Parent ( {
3969+ name : 'Mufasa' ,
3970+ children : [ {
3971+ name : 'Simba' ,
3972+ friends : [ 'Pumbaa' , 'Timon' , 'Nala' ]
3973+ } ]
3974+ } ) ;
3975+
3976+ p . save ( ) .
3977+ then ( function ( p ) {
3978+ assert . equal ( count , 1 ) ;
3979+ p . children [ 0 ] . friends . push ( 'Rafiki' ) ;
3980+ return p . save ( ) ;
3981+ } ) .
3982+ then ( function ( ) {
3983+ assert . equal ( count , 2 ) ;
3984+ done ( ) ;
3985+ } ) .
3986+ catch ( done ) ;
3987+ } ) ;
3988+
39493989 it ( 'does not overwrite when setting nested (gh-4793)' , function ( done ) {
39503990 var grandchildSchema = new mongoose . Schema ( ) ;
39513991 grandchildSchema . method ( {
@@ -5028,6 +5068,7 @@ describe('document', function() {
50285068 assert . ifError ( error ) ;
50295069 var child = doc . children . id ( doc . children [ 0 ] . _id ) ;
50305070 child . phoneNumber = '345' ;
5071+ assert . equal ( contexts . length , 1 ) ;
50315072 doc . save ( function ( error ) {
50325073 assert . ifError ( error ) ;
50335074 assert . equal ( contexts . length , 2 ) ;
0 commit comments