This repository was archived by the owner on Jun 10, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -112,12 +112,16 @@ class ObjectSchema {
112112 }
113113
114114 return objects . reduce ( ( result , object ) => {
115+
115116 this . validate ( object ) ;
117+
116118 for ( const [ key , strategy ] of this [ strategies ] ) {
117119 try {
118- const value = strategy . merge . call ( this , result [ key ] , object [ key ] ) ;
119- if ( value !== undefined ) {
120- result [ key ] = value ;
120+ if ( key in result || key in object ) {
121+ const value = strategy . merge . call ( this , result [ key ] , object [ key ] ) ;
122+ if ( value !== undefined ) {
123+ result [ key ] = value ;
124+ }
121125 }
122126 } catch ( ex ) {
123127 ex . message = `Key "${ key } ": ` + ex . message ;
Original file line number Diff line number Diff line change @@ -102,6 +102,23 @@ describe("ObjectSchema", () => {
102102 assert . propertyVal ( result , "foo" , "bar" ) ;
103103 } ) ;
104104
105+ it ( "should not call the merge() strategy when both objects don't contain the key" , ( ) => {
106+
107+ let called = false ;
108+
109+ schema = new ObjectSchema ( {
110+ foo : {
111+ merge ( ) {
112+ called = true ;
113+ } ,
114+ validate ( ) { }
115+ }
116+ } ) ;
117+
118+ const result = schema . merge ( { } , { } ) ;
119+ assert . isFalse ( called , "The merge() strategy should not have been called." ) ;
120+ } ) ;
121+
105122 it ( "should omit returning the key when the merge() strategy returns undefined" , ( ) => {
106123 schema = new ObjectSchema ( {
107124 foo : {
You can’t perform that action at this time.
0 commit comments