@@ -78,79 +78,81 @@ inherits(ObjectSchema, MixedSchema, {
78
78
, value = MixedSchema . prototype . _cast . call ( schema , _value )
79
79
80
80
//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 ;
86
83
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 ) {
88
91
var exists = has ( value , prop ) ;
89
92
90
- if ( exists && fields [ prop ] ) {
93
+ if ( exists && fields [ prop ] ) {
91
94
var fieldSchema = childSchema ( fields [ prop ] , schema . default ( undefined ) )
92
95
93
96
obj [ prop ] = fieldSchema . cast ( value [ prop ] , { context : obj } )
94
97
}
98
+ else if ( exists && ! strip )
99
+ obj [ prop ] = value [ prop ]
95
100
96
- else if ( exists && ! strip )
97
- obj [ prop ] = cloneDeep ( value [ prop ] )
98
-
99
- else if ( fields [ prop ] ) {
101
+ else if ( fields [ prop ] ) {
100
102
var fieldDefault = fields [ prop ] . default ? fields [ prop ] . default ( ) : undefined
101
103
102
- if ( fieldDefault !== undefined )
104
+ if ( fieldDefault !== undefined )
103
105
obj [ prop ] = fieldDefault
104
106
}
105
-
106
107
} , { } )
107
- }
108
+
109
+ delete schema . _default
110
+ } )
108
111
109
112
return value
110
113
} ,
111
114
112
115
_validate ( _value , _opts , _state ) {
113
116
var errors = [ ]
114
- , context , schema , endEarly , recursive ;
117
+ , state = _state || { }
118
+ , context , schema
119
+ , endEarly , recursive ;
115
120
116
- _state = _state || { }
117
- context = _state . parent || ( _opts || { } ) . context
121
+ context = state . parent || ( _opts || { } ) . context
118
122
schema = this . _resolve ( context )
119
123
endEarly = schema . _option ( 'abortEarly' , _opts )
120
124
recursive = schema . _option ( 'recursive' , _opts )
121
125
122
126
return MixedSchema . prototype . _validate
123
- . call ( this , _value , _opts , _state )
127
+ . call ( this , _value , _opts , state )
124
128
. catch ( endEarly ? null : err => {
125
129
errors . push ( err )
126
130
return err . value
127
131
} )
128
132
. then ( value => {
129
- if ( ! recursive || ! isObject ( value ) ) { // only iterate though actual objects
133
+ if ( ! recursive || ! isObject ( value ) ) { // only iterate though actual objects
130
134
if ( errors . length ) throw errors [ 0 ]
131
135
return value
132
136
}
133
137
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
136
140
, field = childSchema ( schema . fields [ key ] , schema )
137
141
138
142
return field . _validate ( value [ key ]
139
143
, _opts
140
- , { ..._state , key, path, parent : value } )
144
+ , { ...state , key, path, parent : value } )
141
145
} )
142
146
143
147
result = endEarly
144
148
? Promise . all ( result ) . catch ( scopeError ( value ) )
145
- : collectErrors ( result , value , _state . path , errors )
149
+ : collectErrors ( result , value , state . path , errors )
146
150
147
151
return result . then ( ( ) => value )
148
152
} )
149
-
150
-
151
153
} ,
152
154
153
- concat ( schema ) {
155
+ concat ( schema ) {
154
156
var next = MixedSchema . prototype . concat . call ( this , schema )
155
157
156
158
next . _nodes = sortFields ( next . fields , next . _excludedEdges )
@@ -233,8 +235,8 @@ function unknown(ctx, value) {
233
235
function sortFields ( fields , excludes = [ ] ) {
234
236
var edges = [ ] , nodes = [ ]
235
237
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 )
238
240
239
241
fields [ key ] . _deps &&
240
242
fields [ key ] . _deps . forEach ( dep => { //eslint-disable-line no-loop-func
0 commit comments