@@ -83,46 +83,50 @@ inherits(ObjectSchema, MixedSchema, {
8383 value = MixedSchema . prototype . _cast . call ( schema , _value ) ;
8484
8585 //should ignore nulls here
86- if ( schema . _typeCheck ( value ) ) {
87- var fields = schema . fields ,
88- strip = schema . _option ( 'stripUnknown' , _opts ) === true ,
89- extra = Object . keys ( value ) . filter ( function ( v ) {
90- return schema . _nodes . indexOf ( v ) === - 1 ;
91- } ) ,
92- props = schema . _nodes . concat ( extra ) ;
93-
94- return transform ( props , function ( obj , prop ) {
86+ if ( ! schema . _typeCheck ( value ) ) return value ;
87+
88+ var fields = schema . fields ,
89+ strip = schema . _option ( 'stripUnknown' , _opts ) === true ,
90+ extra = Object . keys ( value ) . filter ( function ( v ) {
91+ return schema . _nodes . indexOf ( v ) === - 1 ;
92+ } ) ,
93+ props = schema . _nodes . concat ( extra ) ;
94+
95+ schema . withMutation ( function ( ) {
96+ value = transform ( props , function ( obj , prop ) {
9597 var exists = has ( value , prop ) ;
9698
9799 if ( exists && fields [ prop ] ) {
98100 var fieldSchema = childSchema ( fields [ prop ] , schema [ 'default' ] ( undefined ) ) ;
99101
100102 obj [ prop ] = fieldSchema . cast ( value [ prop ] , { context : obj } ) ;
101- } else if ( exists && ! strip ) obj [ prop ] = cloneDeep ( value [ prop ] ) ; else if ( fields [ prop ] ) {
103+ } else if ( exists && ! strip ) obj [ prop ] = value [ prop ] ; else if ( fields [ prop ] ) {
102104 var fieldDefault = fields [ prop ] [ 'default' ] ? fields [ prop ] [ 'default' ] ( ) : undefined ;
103105
104106 if ( fieldDefault !== undefined ) obj [ prop ] = fieldDefault ;
105107 }
106108 } , { } ) ;
107- }
109+
110+ delete schema . _default ;
111+ } ) ;
108112
109113 return value ;
110114 } ,
111115
112116 _validate : function _validate ( _value , _opts , _state ) {
113117 var errors = [ ] ,
118+ state = _state || { } ,
114119 context ,
115120 schema ,
116121 endEarly ,
117122 recursive ;
118123
119- _state = _state || { } ;
120- context = _state . parent || ( _opts || { } ) . context ;
124+ context = state . parent || ( _opts || { } ) . context ;
121125 schema = this . _resolve ( context ) ;
122126 endEarly = schema . _option ( 'abortEarly' , _opts ) ;
123127 recursive = schema . _option ( 'recursive' , _opts ) ;
124128
125- return MixedSchema . prototype . _validate . call ( this , _value , _opts , _state ) [ 'catch' ] ( endEarly ? null : function ( err ) {
129+ return MixedSchema . prototype . _validate . call ( this , _value , _opts , state ) [ 'catch' ] ( endEarly ? null : function ( err ) {
126130 errors . push ( err ) ;
127131 return err . value ;
128132 } ) . then ( function ( value ) {
@@ -133,13 +137,13 @@ inherits(ObjectSchema, MixedSchema, {
133137 }
134138
135139 var result = schema . _nodes . map ( function ( key ) {
136- var path = ( _state . path ? _state . path + '.' : '' ) + key ,
140+ var path = ( state . path ? state . path + '.' : '' ) + key ,
137141 field = childSchema ( schema . fields [ key ] , schema ) ;
138142
139- return field . _validate ( value [ key ] , _opts , _extends ( { } , _state , { key : key , path : path , parent : value } ) ) ;
143+ return field . _validate ( value [ key ] , _opts , _extends ( { } , state , { key : key , path : path , parent : value } ) ) ;
140144 } ) ;
141145
142- result = endEarly ? Promise . all ( result ) [ 'catch' ] ( scopeError ( value ) ) : collectErrors ( result , value , _state . path , errors ) ;
146+ result = endEarly ? Promise . all ( result ) [ 'catch' ] ( scopeError ( value ) ) : collectErrors ( result , value , state . path , errors ) ;
143147
144148 return result . then ( function ( ) {
145149 return value ;
0 commit comments