@@ -38,10 +38,20 @@ SchemaType.prototype = {
38
38
39
39
constructor : SchemaType ,
40
40
41
- clone ( ) {
41
+ clone ( ) {
42
+ if ( this . _mutate )
43
+ return this ;
44
+
42
45
return cloneDeep ( this ) ;
43
46
} ,
44
47
48
+ withMutation ( fn ) {
49
+ this . _mutate = true
50
+ let result = fn ( this )
51
+ this . _mutate = false
52
+ return result
53
+ } ,
54
+
45
55
concat ( schema ) {
46
56
if ( ! schema )
47
57
return this
@@ -79,26 +89,28 @@ SchemaType.prototype = {
79
89
: this . transforms . reduce (
80
90
( value , transform ) => transform . call ( this , value , _value ) , _value )
81
91
82
-
83
- if ( value === undefined && _ . has ( this , '_default' ) )
92
+ if ( value === undefined && _ . has ( this , '_default' ) )
84
93
value = this . default ( )
85
94
86
95
return value
87
96
} ,
88
97
89
98
_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
+ }
91
103
92
- return this . _deps . reduce ( ( schema , match ) =>
93
- match . resolve ( schema , match . getValue ( parent , context ) ) , schema )
104
+ return this
94
105
} ,
95
106
96
107
//-- tests
97
- _validate ( value , options = { } , state = { } ) {
108
+ _validate ( _value , options = { } , state = { } ) {
98
109
let valids = this . _whitelist
99
110
, invalids = this . _blacklist
100
111
, context = options . context
101
112
, parent = state . parent
113
+ , value = _value
102
114
, schema , endEarly , isStrict ;
103
115
104
116
schema = this . _resolve ( context , parent )
@@ -110,29 +122,29 @@ SchemaType.prototype = {
110
122
let errors = [ ] ;
111
123
let reject = ( ) => Promise . reject ( new ValidationError ( errors , value ) ) ;
112
124
113
- if ( ! state . isCast && ! isStrict )
125
+ if ( ! state . isCast && ! isStrict )
114
126
value = schema . _cast ( value , options )
115
127
116
128
// 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 ) ) {
118
130
errors . push ( schema . _typeError ( { value, path, type : schema . _type } ) )
119
131
if ( endEarly ) return reject ( )
120
132
}
121
133
122
134
// next check Whitelist for matching values
123
- if ( valids . length && ! valids . has ( value ) ) {
135
+ if ( valids . length && ! valids . has ( value ) ) {
124
136
errors . push ( schema . _whitelistError ( valids . values ( ) , path ) )
125
- if ( endEarly ) return reject ( )
137
+ if ( endEarly ) return reject ( )
126
138
}
127
139
128
140
// next check Blacklist for matching values
129
- if ( invalids . has ( value ) ) {
141
+ if ( invalids . has ( value ) ) {
130
142
errors . push ( schema . _blacklistError ( invalids . values ( ) , path ) )
131
- if ( endEarly ) return reject ( )
143
+ if ( endEarly ) return reject ( )
132
144
}
133
145
134
146
// It makes no sense to validate further at this point if their are errors
135
- if ( errors . length )
147
+ if ( errors . length )
136
148
return reject ( )
137
149
138
150
let result = schema . tests . map ( fn => fn ( { value, path, state, schema, options } ) )
0 commit comments