@@ -78,79 +78,81 @@ inherits(ObjectSchema, MixedSchema, {
7878 , value = MixedSchema . prototype . _cast . call ( schema , _value )
7979
8080 //should ignore nulls here
81- if ( schema . _typeCheck ( value ) ) {
82- var fields = schema . fields
83- , strip = schema . _option ( 'stripUnknown' , _opts ) === true
84- , extra = Object . keys ( value ) . filter ( v => schema . _nodes . indexOf ( v ) === - 1 )
85- , props = schema . _nodes . concat ( extra )
81+ if ( ! schema . _typeCheck ( value ) )
82+ return value ;
8683
87- return transform ( props , function ( obj , prop ) {
84+ var fields = schema . fields
85+ , strip = schema . _option ( 'stripUnknown' , _opts ) === true
86+ , extra = Object . keys ( value ) . filter ( v => schema . _nodes . indexOf ( v ) === - 1 )
87+ , props = schema . _nodes . concat ( extra ) ;
88+
89+ schema . withMutation ( ( ) => {
90+ value = transform ( props , function ( obj , prop ) {
8891 var exists = has ( value , prop ) ;
8992
90- if ( exists && fields [ prop ] ) {
93+ if ( exists && fields [ prop ] ) {
9194 var fieldSchema = childSchema ( fields [ prop ] , schema . default ( undefined ) )
9295
9396 obj [ prop ] = fieldSchema . cast ( value [ prop ] , { context : obj } )
9497 }
98+ else if ( exists && ! strip )
99+ obj [ prop ] = value [ prop ]
95100
96- else if ( exists && ! strip )
97- obj [ prop ] = cloneDeep ( value [ prop ] )
98-
99- else if ( fields [ prop ] ) {
101+ else if ( fields [ prop ] ) {
100102 var fieldDefault = fields [ prop ] . default ? fields [ prop ] . default ( ) : undefined
101103
102- if ( fieldDefault !== undefined )
104+ if ( fieldDefault !== undefined )
103105 obj [ prop ] = fieldDefault
104106 }
105-
106107 } , { } )
107- }
108+
109+ delete schema . _default
110+ } )
108111
109112 return value
110113 } ,
111114
112115 _validate ( _value , _opts , _state ) {
113116 var errors = [ ]
114- , context , schema , endEarly , recursive ;
117+ , state = _state || { }
118+ , context , schema
119+ , endEarly , recursive ;
115120
116- _state = _state || { }
117- context = _state . parent || ( _opts || { } ) . context
121+ context = state . parent || ( _opts || { } ) . context
118122 schema = this . _resolve ( context )
119123 endEarly = schema . _option ( 'abortEarly' , _opts )
120124 recursive = schema . _option ( 'recursive' , _opts )
121125
122126 return MixedSchema . prototype . _validate
123- . call ( this , _value , _opts , _state )
127+ . call ( this , _value , _opts , state )
124128 . catch ( endEarly ? null : err => {
125129 errors . push ( err )
126130 return err . value
127131 } )
128132 . then ( value => {
129- if ( ! recursive || ! isObject ( value ) ) { // only iterate though actual objects
133+ if ( ! recursive || ! isObject ( value ) ) { // only iterate though actual objects
130134 if ( errors . length ) throw errors [ 0 ]
131135 return value
132136 }
133137
134- let result = schema . _nodes . map ( function ( key ) {
135- var path = ( _state . path ? ( _state . path + '.' ) : '' ) + key
138+ let result = schema . _nodes . map ( function ( key ) {
139+ var path = ( state . path ? ( state . path + '.' ) : '' ) + key
136140 , field = childSchema ( schema . fields [ key ] , schema )
137141
138142 return field . _validate ( value [ key ]
139143 , _opts
140- , { ..._state , key, path, parent : value } )
144+ , { ...state , key, path, parent : value } )
141145 } )
142146
143147 result = endEarly
144148 ? Promise . all ( result ) . catch ( scopeError ( value ) )
145- : collectErrors ( result , value , _state . path , errors )
149+ : collectErrors ( result , value , state . path , errors )
146150
147151 return result . then ( ( ) => value )
148152 } )
149-
150-
151153 } ,
152154
153- concat ( schema ) {
155+ concat ( schema ) {
154156 var next = MixedSchema . prototype . concat . call ( this , schema )
155157
156158 next . _nodes = sortFields ( next . fields , next . _excludedEdges )
@@ -233,8 +235,8 @@ function unknown(ctx, value) {
233235function sortFields ( fields , excludes = [ ] ) {
234236 var edges = [ ] , nodes = [ ]
235237
236- for ( var key in fields ) if ( has ( fields , key ) ) {
237- if ( ! ~ nodes . indexOf ( key ) ) nodes . push ( key )
238+ for ( var key in fields ) if ( has ( fields , key ) ) {
239+ if ( ! ~ nodes . indexOf ( key ) ) nodes . push ( key )
238240
239241 fields [ key ] . _deps &&
240242 fields [ key ] . _deps . forEach ( dep => { //eslint-disable-line no-loop-func
0 commit comments