@@ -10,10 +10,14 @@ var Promise = require('promise/lib/es6-extensions')
10
10
, createValidation = require ( './util/createValidation' )
11
11
, BadSet = require ( './util/set' ) ;
12
12
13
- let formatError = ValidationError . formatError
14
-
15
13
let notEmpty = value => ! isAbsent ( value ) ;
16
14
15
+ function runValidations ( validations , endEarly , value , path ) {
16
+ return endEarly
17
+ ? Promise . all ( validations )
18
+ : _ . collectErrors ( validations , value , path )
19
+ }
20
+
17
21
module . exports = SchemaType
18
22
19
23
function SchemaType ( options = { } ) {
@@ -27,7 +31,10 @@ function SchemaType(options = {}){
27
31
this . _blacklist = new BadSet ( )
28
32
this . tests = [ ]
29
33
this . transforms = [ ]
30
- this . _typeError = formatError ( locale . notType )
34
+
35
+ this . withMutation ( ( ) => {
36
+ this . typeError ( locale . notType )
37
+ } )
31
38
32
39
if ( _ . has ( options , 'default' ) )
33
40
this . _defaultDefault = options . default
@@ -114,9 +121,7 @@ SchemaType.prototype = {
114
121
115
122
//-- tests
116
123
_validate ( _value , options = { } , state = { } ) {
117
- let valids = this . _whitelist
118
- , invalids = this . _blacklist
119
- , context = options . context
124
+ let context = options . context
120
125
, parent = state . parent
121
126
, value = _value
122
127
, schema , endEarly , isStrict ;
@@ -127,41 +132,28 @@ SchemaType.prototype = {
127
132
128
133
let path = state . path
129
134
130
- let errors = [ ] ;
131
- let reject = ( ) => Promise . reject ( new ValidationError ( errors , value ) ) ;
132
-
133
135
if ( ! state . isCast && ! isStrict )
134
136
value = schema . _cast ( value , options )
135
137
136
138
// value is cast, we can check if it meets type requirements
137
- if ( value !== undefined && ! schema . isType ( value ) ) {
138
- errors . push ( schema . _typeError ( { value, path, type : schema . _type } ) )
139
- if ( endEarly ) return reject ( )
140
- }
141
-
142
- // next check Whitelist for matching values
143
- if ( valids . length && ! valids . has ( value ) ) {
144
- errors . push ( schema . _whitelistError ( valids . values ( ) , path ) )
145
- if ( endEarly ) return reject ( )
146
- }
147
-
148
- // next check Blacklist for matching values
149
- if ( invalids . has ( value ) ) {
150
- errors . push ( schema . _blacklistError ( invalids . values ( ) , path ) )
151
- if ( endEarly ) return reject ( )
152
- }
153
-
154
- // It makes no sense to validate further at this point if their are errors
155
- if ( errors . length )
156
- return reject ( )
157
-
158
- let result = schema . tests . map ( fn => fn ( { value, path, state, schema, options } ) )
159
-
160
- result = endEarly
161
- ? Promise . all ( result )
162
- : _ . collectErrors ( result , value , path )
163
-
164
- return result . then ( ( ) => value ) ;
139
+ let validationParams = { value, path, state, schema, options }
140
+ let initialTests = [ ]
141
+
142
+ if ( schema . _typeError )
143
+ initialTests . push ( schema . _typeError ( validationParams ) ) ;
144
+ if ( schema . _whitelistError )
145
+ initialTests . push ( schema . _whitelistError ( validationParams ) ) ;
146
+ if ( schema . _blacklistError )
147
+ initialTests . push ( schema . _blacklistError ( validationParams ) ) ;
148
+
149
+ return runValidations ( initialTests , endEarly , value , path )
150
+ . then ( ( ) => runValidations (
151
+ schema . tests . map ( fn => fn ( validationParams ) )
152
+ , endEarly
153
+ , value
154
+ , path
155
+ ) )
156
+ . then ( ( ) => value )
165
157
} ,
166
158
167
159
validate ( value , options , cb ) {
@@ -212,12 +204,6 @@ SchemaType.prototype = {
212
204
)
213
205
} ,
214
206
215
- typeError ( msg ) {
216
- var next = this . clone ( )
217
- next . _typeError = formatError ( msg )
218
- return next
219
- } ,
220
-
221
207
nullable ( value ) {
222
208
var next = this . clone ( )
223
209
next . _nullable = value === false ? false : true
@@ -296,34 +282,67 @@ SchemaType.prototype = {
296
282
return next
297
283
} ,
298
284
299
- oneOf ( enums , msg ) {
285
+ typeError ( message ) {
300
286
var next = this . clone ( )
301
287
302
- if ( next . tests . length )
303
- throw new TypeError ( 'Cannot specify values when there are validation rules specified' )
288
+ next . _typeError = createValidation ( {
289
+ message,
290
+ name : 'typeError' ,
291
+ test ( value ) {
292
+ if ( value !== undefined && ! this . schema . isType ( value ) )
293
+ return this . createError ( {
294
+ params : { type : this . schema . _type }
295
+ } )
296
+ return true
297
+ }
298
+ } )
299
+ return next
300
+ } ,
304
301
305
- next . _whitelistError = ( valids , path ) =>
306
- formatError ( msg || locale . oneOf , { values : valids . join ( ', ' ) , path } )
302
+ oneOf ( enums , message = locale . oneOf ) {
303
+ var next = this . clone ( ) ;
307
304
308
- enums . forEach ( val => {
305
+ if ( next . tests . length )
306
+ throw new TypeError ( 'Cannot specify values when there are validation rules specified' )
307
+
308
+ enums . forEach ( val => {
309
309
next . _blacklist . delete ( val )
310
310
next . _whitelist . add ( val )
311
311
} )
312
312
313
+ next . _whitelistError = createValidation ( {
314
+ message,
315
+ name : 'oneOf' ,
316
+ test ( value ) {
317
+ let valids = this . schema . _whitelist
318
+ if ( valids . length && ! valids . has ( value ) )
319
+ return this . createError ( { params : { values : valids . values ( ) . join ( ', ' ) } } )
320
+ return true
321
+ }
322
+ } )
323
+
313
324
return next
314
325
} ,
315
326
316
- notOneOf ( enums , msg ) {
317
- var next = this . clone ( )
318
-
319
- next . _blacklistError = ( invalids , path ) =>
320
- formatError ( msg || locale . notOneOf , { values : invalids . join ( ', ' ) , path } )
327
+ notOneOf ( enums , message = locale . notOneOf ) {
328
+ var next = this . clone ( ) ;
321
329
322
330
enums . forEach ( val => {
323
331
next . _whitelist . delete ( val )
324
332
next . _blacklist . add ( val )
325
333
} )
326
334
335
+ next . _blacklistError = createValidation ( {
336
+ message,
337
+ name : 'notOneOf' ,
338
+ test ( value ) {
339
+ let invalids = this . schema . _blacklist
340
+ if ( invalids . length && invalids . has ( value ) )
341
+ return this . createError ( { params : { values : invalids . values ( ) . join ( ', ' ) } } )
342
+ return true
343
+ }
344
+ } )
345
+
327
346
return next
328
347
} ,
329
348
0 commit comments