@@ -111,8 +111,18 @@ SchemaType.prototype = {
111111 return ! this . _typeCheck || this . _typeCheck ( v )
112112 } ,
113113
114+ resolve ( context , parent ) {
115+ if ( this . _conditions . length ) {
116+ return this . _conditions . reduce ( ( schema , match ) =>
117+ match . resolve ( schema , match . getValue ( parent , context ) ) , this )
118+ }
119+
120+ return this
121+ } ,
122+
114123 cast ( value , opts = { } ) {
115- var schema = this . _resolve ( opts . context , opts . parent )
124+ let schema = this . resolve ( opts . context , opts . parent )
125+
116126 return schema . _cast ( value , opts )
117127 } ,
118128
@@ -121,67 +131,60 @@ SchemaType.prototype = {
121131 : this . transforms . reduce (
122132 ( value , transform ) => transform . call ( this , value , _value ) , _value )
123133
124- if ( value === undefined && _ . has ( this , '_default' ) )
134+ if ( value === undefined && ( _ . has ( this , '_default' ) ) ) {
125135 value = this . default ( )
136+ }
126137
127138 return value
128139 } ,
129140
130- _resolve ( context , parent ) {
131- if ( this . _conditions . length ) {
132- return this . _conditions . reduce ( ( schema , match ) =>
133- match . resolve ( schema , match . getValue ( parent , context ) ) , this )
134- }
141+ validate ( value , options = { } , cb ) {
142+ if ( typeof options === 'function' )
143+ cb = options , options = { }
135144
136- return this
145+ let schema = this . resolve ( options . context , options . parent )
146+
147+ return nodeify ( schema . _validate ( value , options ) , cb )
137148 } ,
138149
139150 //-- tests
140- _validate ( _value , options = { } , state = { } ) {
141- let context = options . context
142- , parent = state . parent
143- , value = _value
151+ _validate ( _value , options = { } ) {
152+ let value = _value
144153 , schema , endEarly , isStrict ;
145154
146- schema = this . _resolve ( context , parent )
147- isStrict = schema . _option ( 'strict' , options )
148- endEarly = schema . _option ( 'abortEarly' , options )
155+ schema = this
156+ isStrict = this . _option ( 'strict' , options )
157+ endEarly = this . _option ( 'abortEarly' , options )
149158
150- let path = state . path
159+ let path = options . path
151160 let label = this . _label
152161
153- if ( ! state . isCast && ! isStrict )
154- value = schema . _cast ( value , options )
155-
162+ if ( ! isStrict ) {
163+ value = this . _cast ( value , options , options )
164+ }
156165 // value is cast, we can check if it meets type requirements
157- let validationParams = { value, path, state , schema, options, label }
166+ let validationParams = { value, path, schema : this , options, label }
158167 let initialTests = [ ]
159168
160169 if ( schema . _typeError )
161- initialTests . push ( schema . _typeError ( validationParams ) ) ;
170+ initialTests . push ( this . _typeError ( validationParams ) ) ;
162171
163- if ( schema . _whitelistError )
164- initialTests . push ( schema . _whitelistError ( validationParams ) ) ;
172+ if ( this . _whitelistError )
173+ initialTests . push ( this . _whitelistError ( validationParams ) ) ;
165174
166- if ( schema . _blacklistError )
167- initialTests . push ( schema . _blacklistError ( validationParams ) ) ;
175+ if ( this . _blacklistError )
176+ initialTests . push ( this . _blacklistError ( validationParams ) ) ;
168177
169178 return runValidations ( initialTests , endEarly , value , path )
170179 . then ( ( ) => runValidations (
171- schema . tests . map ( fn => fn ( validationParams ) )
180+ this . tests . map ( fn => fn ( validationParams ) )
172181 , endEarly
173182 , value
174183 , path
175184 ) )
176185 . then ( ( ) => value )
177186 } ,
178187
179- validate ( value , options , cb ) {
180- if ( typeof options === 'function' )
181- cb = options , options = { }
182-
183- return nodeify ( this . _validate ( value , options , { } ) , cb )
184- } ,
185188
186189 isValid ( value , options , cb ) {
187190 if ( typeof options === 'function' )
0 commit comments