@@ -38,10 +38,20 @@ SchemaType.prototype = {
3838
3939 constructor : SchemaType ,
4040
41- clone ( ) {
41+ clone ( ) {
42+ if ( this . _mutate )
43+ return this ;
44+
4245 return cloneDeep ( this ) ;
4346 } ,
4447
48+ withMutation ( fn ) {
49+ this . _mutate = true
50+ let result = fn ( this )
51+ this . _mutate = false
52+ return result
53+ } ,
54+
4555 concat ( schema ) {
4656 if ( ! schema )
4757 return this
@@ -79,26 +89,28 @@ SchemaType.prototype = {
7989 : this . transforms . reduce (
8090 ( value , transform ) => transform . call ( this , value , _value ) , _value )
8191
82-
83- if ( value === undefined && _ . has ( this , '_default' ) )
92+ if ( value === undefined && _ . has ( this , '_default' ) )
8493 value = this . default ( )
8594
8695 return value
8796 } ,
8897
8998 _resolve ( context , parent ) {
90- var schema = this ;
99+ if ( this . _deps . length ) {
100+ return this . _deps . reduce ( ( schema , match ) =>
101+ match . resolve ( schema , match . getValue ( parent , context ) ) , this )
102+ }
91103
92- return this . _deps . reduce ( ( schema , match ) =>
93- match . resolve ( schema , match . getValue ( parent , context ) ) , schema )
104+ return this
94105 } ,
95106
96107 //-- tests
97- _validate ( value , options = { } , state = { } ) {
108+ _validate ( _value , options = { } , state = { } ) {
98109 let valids = this . _whitelist
99110 , invalids = this . _blacklist
100111 , context = options . context
101112 , parent = state . parent
113+ , value = _value
102114 , schema , endEarly , isStrict ;
103115
104116 schema = this . _resolve ( context , parent )
@@ -110,29 +122,29 @@ SchemaType.prototype = {
110122 let errors = [ ] ;
111123 let reject = ( ) => Promise . reject ( new ValidationError ( errors , value ) ) ;
112124
113- if ( ! state . isCast && ! isStrict )
125+ if ( ! state . isCast && ! isStrict )
114126 value = schema . _cast ( value , options )
115127
116128 // value is cast, we can check if it meets type requirements
117- if ( value !== undefined && ! schema . isType ( value ) ) {
129+ if ( value !== undefined && ! schema . isType ( value ) ) {
118130 errors . push ( schema . _typeError ( { value, path, type : schema . _type } ) )
119131 if ( endEarly ) return reject ( )
120132 }
121133
122134 // next check Whitelist for matching values
123- if ( valids . length && ! valids . has ( value ) ) {
135+ if ( valids . length && ! valids . has ( value ) ) {
124136 errors . push ( schema . _whitelistError ( valids . values ( ) , path ) )
125- if ( endEarly ) return reject ( )
137+ if ( endEarly ) return reject ( )
126138 }
127139
128140 // next check Blacklist for matching values
129- if ( invalids . has ( value ) ) {
141+ if ( invalids . has ( value ) ) {
130142 errors . push ( schema . _blacklistError ( invalids . values ( ) , path ) )
131- if ( endEarly ) return reject ( )
143+ if ( endEarly ) return reject ( )
132144 }
133145
134146 // It makes no sense to validate further at this point if their are errors
135- if ( errors . length )
147+ if ( errors . length )
136148 return reject ( )
137149
138150 let result = schema . tests . map ( fn => fn ( { value, path, state, schema, options } ) )
0 commit comments