@@ -14,30 +14,19 @@ var _require = require('./util/_');
14
14
15
15
var isObject = _require . isObject ;
16
16
var transform = _require . transform ;
17
- var assign = _require . assign ;
18
17
var inherits = _require . inherits ;
19
18
var collectErrors = _require . collectErrors ;
20
19
var isSchema = _require . isSchema ;
21
20
var has = _require . has ;
22
21
23
22
24
- var isRecursive = function isRecursive ( schema ) {
25
- return ( schema . _subType || schema ) === '$this' ;
26
- } ;
27
-
28
23
c . type ( 'altCamel' , function ( str ) {
29
24
var result = c . camel ( str ) ,
30
25
idx = str . search ( / [ ^ _ ] / ) ;
31
26
32
27
return idx === 0 ? result : str . substr ( 0 , idx ) + result ;
33
28
} ) ;
34
29
35
- var childSchema = function childSchema ( field , parent ) {
36
- if ( ! isRecursive ( field ) ) return field ;
37
-
38
- return field . of ? field . of ( parent ) : parent ;
39
- } ;
40
-
41
30
var scopeError = function scopeError ( value ) {
42
31
return function ( err ) {
43
32
err . value = value ;
@@ -89,64 +78,58 @@ inherits(ObjectSchema, MixedSchema, {
89
78
return isObject ( value ) || typeof value === 'function' ;
90
79
} ,
91
80
_cast : function _cast ( _value ) {
92
- var _opts = arguments . length <= 1 || arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
81
+ var _this3 = this ;
93
82
94
- var schema = this ,
95
- value = MixedSchema . prototype . _cast . call ( schema , _value ) ;
83
+ var opts = arguments . length <= 1 || arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
84
+
85
+ var value = MixedSchema . prototype . _cast . call ( this , _value , opts ) ;
96
86
97
87
//should ignore nulls here
98
- if ( ! schema . _typeCheck ( value ) ) return value ;
88
+ if ( value === undefined ) return this . default ( ) ;
89
+
90
+ if ( ! this . _typeCheck ( value ) ) return value ;
99
91
100
- var fields = schema . fields ,
101
- strip = schema . _option ( 'stripUnknown' , _opts ) === true ,
92
+ var fields = this . fields ,
93
+ strip = this . _option ( 'stripUnknown' , opts ) === true ,
102
94
extra = Object . keys ( value ) . filter ( function ( v ) {
103
- return schema . _nodes . indexOf ( v ) === - 1 ;
95
+ return _this3 . _nodes . indexOf ( v ) === - 1 ;
104
96
} ) ,
105
- props = schema . _nodes . concat ( extra ) ;
97
+ props = this . _nodes . concat ( extra ) ;
106
98
107
- schema . withMutation ( function ( ) {
108
- var innerOptions = _extends ( { } , _opts , { parent : { } } ) ;
99
+ var innerOptions = _extends ( { } , opts , { parent : { } } ) ;
109
100
110
- value = transform ( props , function ( obj , prop ) {
111
- var field = fields [ prop ] ;
112
- var exists = has ( value , prop ) ;
101
+ value = transform ( props , function ( obj , prop ) {
102
+ var field = fields [ prop ] ;
103
+ var exists = has ( value , prop ) ;
113
104
114
- if ( Ref . isRef ( field ) ) {
115
- var refValue = field . getValue ( obj , innerOptions . context ) ;
105
+ if ( field ) {
106
+ var fieldValue = void 0 ;
116
107
117
- if ( refValue !== undefined ) obj [ prop ] = refValue ;
118
- } else if ( exists && field ) {
119
- tempClearDefault ( schema , function ( ) {
120
- var fieldSchema = childSchema ( field , schema . default ( undefined ) ) ;
108
+ if ( field . _strip === true ) return ;
121
109
122
- if ( fieldSchema . _strip !== true ) {
123
- obj [ prop ] = fieldSchema . cast ( value [ prop ] , innerOptions ) ;
124
- }
125
- } ) ;
126
- } else if ( exists && ! strip ) obj [ prop ] = value [ prop ] ; else if ( field ) {
127
- var fieldDefault = field . default ? field . default ( ) : undefined ;
110
+ fieldValue = field . cast ( value [ prop ] , innerOptions ) ;
128
111
129
- if ( fieldDefault !== undefined ) obj [ prop ] = fieldDefault ;
130
- }
131
- } , innerOptions . parent ) ;
132
- } ) ;
112
+ if ( fieldValue !== undefined ) obj [ prop ] = fieldValue ;
113
+ } else if ( exists && ! strip ) obj [ prop ] = value [ prop ] ;
114
+ } , innerOptions . parent ) ;
133
115
134
116
return value ;
135
117
} ,
136
- _validate : function _validate ( _value , _opts , _state ) {
118
+ _validate : function _validate ( _value ) {
119
+ var _this4 = this ;
120
+
121
+ var opts = arguments . length <= 1 || arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
122
+
137
123
var errors = [ ] ,
138
- state = _state || { } ,
139
- context ,
140
- schema ,
141
124
endEarly ,
125
+ isStrict ,
142
126
recursive ;
143
127
144
- context = state . parent || ( _opts || { } ) . context ;
145
- schema = this . _resolve ( context ) ;
146
- endEarly = schema . _option ( 'abortEarly' , _opts ) ;
147
- recursive = schema . _option ( 'recursive' , _opts ) ;
128
+ isStrict = this . _option ( 'strict' , opts ) ;
129
+ endEarly = this . _option ( 'abortEarly' , opts ) ;
130
+ recursive = this . _option ( 'recursive' , opts ) ;
148
131
149
- return MixedSchema . prototype . _validate . call ( this , _value , _opts , state ) . catch ( endEarly ? null : function ( err ) {
132
+ return MixedSchema . prototype . _validate . call ( this , _value , opts ) . catch ( endEarly ? null : function ( err ) {
150
133
errors . push ( err ) ;
151
134
return err . value ;
152
135
} ) . then ( function ( value ) {
@@ -156,14 +139,24 @@ inherits(ObjectSchema, MixedSchema, {
156
139
return value ;
157
140
}
158
141
159
- var result = schema . _nodes . map ( function ( key ) {
160
- var path = ( state . path ? state . path + '.' : '' ) + key ,
161
- field = childSchema ( schema . fields [ key ] , schema ) ;
142
+ var result = _this4 . _nodes . map ( function ( key ) {
143
+ var path = ( opts . path ? opts . path + '.' : '' ) + key ,
144
+ field = _this4 . fields [ key ] ,
145
+ innerOptions = _extends ( { } , opts , { key : key , path : path , parent : value } ) ;
146
+
147
+ if ( field ) {
148
+ // inner fields are always strict:
149
+ // 1. this isn't strict so we just cast the value leaving nested values already cast
150
+ // 2. this is strict in which case the nested values weren't cast either
151
+ innerOptions . strict = true ;
152
+
153
+ if ( field . validate ) return field . validate ( value [ key ] , innerOptions ) ;
154
+ }
162
155
163
- return field . _validate ( value [ key ] , _opts , _extends ( { } , state , { key : key , path : path , parent : value } ) ) ;
156
+ return true ;
164
157
} ) ;
165
158
166
- result = endEarly ? Promise . all ( result ) . catch ( scopeError ( value ) ) : collectErrors ( result , value , state . path , errors ) ;
159
+ result = endEarly ? Promise . all ( result ) . catch ( scopeError ( value ) ) : collectErrors ( result , value , opts . path , errors ) ;
167
160
168
161
return result . then ( function ( ) {
169
162
return value ;
@@ -181,7 +174,7 @@ inherits(ObjectSchema, MixedSchema, {
181
174
var excludes = arguments . length <= 1 || arguments [ 1 ] === undefined ? [ ] : arguments [ 1 ] ;
182
175
183
176
var next = this . clone ( ) ,
184
- fields = assign ( next . fields , schema ) ;
177
+ fields = _extends ( next . fields , schema ) ;
185
178
186
179
if ( ! Array . isArray ( excludes [ 0 ] ) ) excludes = [ excludes ] ;
187
180
@@ -257,14 +250,15 @@ function unknown(ctx, value) {
257
250
258
251
// ugly optimization avoiding a clone. clears default for recursive
259
252
// cast and resets it below;
260
- function tempClearDefault ( schema , fn ) {
261
- var hasDflt = has ( schema , '_default' ) ,
262
- dflt = schema . _default ;
263
-
264
- fn ( schema ) ;
265
-
266
- if ( hasDflt ) schema . default ( dflt ) ; else delete schema . _default ;
267
- }
253
+ // function tempClearDefault(schema, fn) {
254
+ // let hasDflt = has(schema, '_default')
255
+ // , dflt = schema._default;
256
+ //
257
+ // fn(schema)
258
+ //
259
+ // if (hasDflt) schema.default(dflt)
260
+ // else delete schema._default
261
+ // }
268
262
269
263
function sortFields ( fields ) {
270
264
var excludes = arguments . length <= 1 || arguments [ 1 ] === undefined ? [ ] : arguments [ 1 ] ;
0 commit comments