-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
930 lines (789 loc) · 24.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
var Promise = this.Promise || require("promise")
var Events = require("events").EventEmitter;
var dateUtil = require("./date-util");
var BUILD_IN_RULE = {
isEmail: function(email){
return verifyIsEmail(email, {});
},
isUrl: verifyIsUrl,
isNumber: verifyIsNumber,
isTel: verifyIsTel,
isColor: verifyIsColor,
isDate: verifyIsDate,
isDateTime: verifyIsDateTime,
isDateTimeLocal: verifyIsDateTimeLocal,
isMonth: verifyIsMonth,
isWeek: verifyIsWeek,
isTime: verifyIsTime,
isMobile: function(mobile){
return verifyIsMobile(mobile, {});
}
};
var RULE_TYPES = {
"text": "text",
"password": "password",
"email": "email",
"radio": "radio",
"checkbox": "checkbox",
"select-one": "select-one",
"select-multiple": "select-multiple",
"hidden": "hidden",
"search": "search",
"textarea": "textarea",
"file": "file",
"number": "number",
"range": "range",
"date": "date",
"week": "week",
"month": "month",
"time": "time",
"datetime": "datetime",
"datetime-local": "datetime-local",
"url": "url",
"tel": "tel",
"color": "color",
"submit": "submit",
"button": "button",
"reset": "reset",
"image": "image",
"fieldset": "fieldset",
"legend": "legend"
};
// @param {Object} object.
// @param {String} type, like `Array`, `RegExp`, etc.
function typeOf(type){
return function(object){
return Object.prototype.toString.call(object) === "[object " + type + "]";
}
}
var isString = typeOf("String")
var isBoolean = typeOf("Boolean")
var isArray = typeOf("Array")
var isRegExp = typeOf("RegExp")
var isFunction = typeOf("Function")
var _isNumber = typeOf("Number")
var _isObject = typeOf("Object")
function isNumber(object){
return !isNaN(object) && _isNumber(object);
}
function isObject(object){
return null !== object && _isObject(object);
}
function isPromise(object) {
return object && isFunction(object.then);
}
// #12, 同时支持 Promise 和非 Promise。
// 对于 Promise,等待其兑现。
// 对于非 Promise,立即执行,提高性能。
function UniPromise (promise, resolve, reject) {
if (isPromise(promise)) {
return promise.then(resolve, reject)
} else {
return resolve(promise)
}
}
function trim(string){
return String(string).replace(/^\s+/, "").replace(/\s+$/, "");
}
// @param {Object} object.
// @return {Number} return number if object can convert to number,
// else return NaN.
function toNumber(object){
if(isNumber(object)){return object;}
object = trim(object);
if("" === object){return NaN;}
return Number(object);
}
// @param {Object} rules
// @param {Function} handler
// @param {Function} placehandler, optional. When has not item, call placehorder handler.
function eachRules(rules, handler, placehandler){
var hasRule = false;
for(var ruleName in rules){
if(rules.hasOwnProperty(ruleName)){
hasRule = true;
handler.call(rules, ruleName, rules[ruleName]);
}
}
if (!hasRule && isFunction(placehandler)) {
placehandler()
}
}
function eachValues(handler, values /* ,... */){
var certified = true;
var args = Array.prototype.slice.call(arguments, 0).slice(1);
if(isArray(values)){
for(var i=0,l=values.length; i<l; i++){
args[0] = values[i];
certified = certified && handler.apply(null, args);
}
return certified;
}
return handler.apply(null, args);
}
function merge(/* ... */){
var result = {};
for(var i=0,object,l=arguments.length; i<l; i++){
object = arguments[i];
if(!isObject(object)){continue;}
for(var key in object){
if(object.hasOwnProperty(key)){
result[key] = object[key]
}
}
}
return result;
}
function startsWith(string, prefix){
return isString(string) && string.indexOf(prefix) === 0;
}
function endsWith(string, suffix) {
return isString(string) &&
string.indexOf(suffix, string.length - suffix.length) !== -1;
}
// 通常情况下的 required 校验。
// @param {Boolean,Undefined} required, is rule required?
// @param {String,Object} values, validation data.
// @return {Boolean,Undefined}
// if !values and required, return false;
// if !values and not-required, return true;
// if values, validate passed, and continue next, return undefined.
function verifyRequired(required, values){
if(isArray(values)){
if(!verifyMinLimit(1, values)){
return !isBoolean(required) || !required;
}
}else{
if("undefined"===typeof values || null===values || ""===values){
return !isBoolean(required) || !required;
}
}
//!return undefined;
}
function verifyIsNumber(value, validity){
var certified = /^[+-]?\d+(?:[eE][+-]?\d+)?$/.test(value) ||
/^[+-]?(?:\d+)?\.\d+(?:[eE][+-]?\d+)?$/.test(value);
validity.typeMismatch = !certified;
return certified;
}
function verifyMin(value, min, validity){
var certified;
value = toNumber(value);
min = toNumber(min);
if(!isNumber(min)){
certified = true;
} else if(!isNumber(value)){
certified = false;
} else {
certified = value >= min;
}
validity.rangeUnderflow = !certified;
return certified;
//return !isNumber(min) || isNumber(value) && Number(value) >= Number(min);
}
function verifyMax(value, max, validity){
var certified;
value = toNumber(value);
max = toNumber(max);
if(!isNumber(max)){
certified = true;
} else if(!isNumber(value)){
certified = false;
} else {
certified = value <= max;
}
// XXX: Non-Effect.
validity.rangeOverflow = !certified;
return certified;
//return !isNumber(max) || isNumber(value) && Number(value) <= Number(max);
}
function verifyMinLimit(minlimit, values, validity){
var certified;
var length = 0;
minlimit = toNumber(minlimit);
if(!isNumber(minlimit)){return true;}
if(!isArray(values)){
values = [ values ];
}
if(values.length < minlimit){
certified = false;
} else {
for(var i=0,l=values.length; i<l; i++){
if("undefined"!==typeof values[i] && null!==values[i] && ""!==values[i]){
length++;
}
}
}
certified = length >= minlimit;
if (validity) {
// XXX: tooShort? no. rangeUnderflow? no. is tooFew.
validity.customError = !certified;
}
return certified;
}
function verifyMaxLimit(maxlimit, values, validity){
maxlimit = toNumber(maxlimit);
if(!isNumber(maxlimit)){return true;}
if(!isArray(values)){
values = [ values ];
}
var length = 0;
for(var i=0,l=values.length; i<l; i++){
if("undefined"!==typeof values[i] && null!==values[i] && ""!==values[i]){
length++;
}
}
var certified = length <= maxlimit;
if (validity) {
// XXX: tooLong? no. rangeOverflow? no. is tooMuch.
validity.customError = !certified;
}
return certified;
}
function verifyMinLengthList(minlength, values, validity){
minlength = toNumber(minlength);
if(!isNumber(minlength)){return true;}
var certified = true;
for(var i=0,l=values.length; i<l; i++){
certified = certified && verifyMinLength(minlength, values[i], validity);
}
return certified;
}
function verifyMaxLengthList(maxlength, values, validity){
maxlength = toNumber(maxlength);
if(!isNumber(maxlength)){return true;}
var certified = true;
for(var i=0,l=values.length; i<l; i++){
certified = certified && verifyMaxLength(maxlength, values[i], validity);
}
return certified;
}
function verifyMinLength(minlength, value, validity){
minlength = toNumber(minlength);
var certified = !isNumber(minlength) ||
(isString(value) && value.length >= minlength);
validity.tooShort = !certified;
return certified;
}
function verifyMaxLength(maxlength, value, validity){
maxlength = toNumber(maxlength);
var certified = !isNumber(maxlength) ||
(isString(value) && value.length <= maxlength);
validity.tooLong = !certified;
return certified;
}
function verifyIsMonth(value, validity){
var certified = dateUtil.isMonth(value);
validity.typeMismatch = !certified;
return certified;
}
function verifyMinMonth(value, min, instance_context, validity){
if(!min){return true;}
if(!verifyIsMonth(min, validity)){
instance_context._evt.emit("error",
new TypeError('[type=month][min='+min+'] is invalid month.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxMonth(value, max, instance_context, validity){
if(!max){return true;}
if(!verifyIsMonth(max, validity)){
instance_context._evt.emit("error",
new TypeError('[type=month][max='+max+'] is invalid month.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
validity.rangeOverflow = !certified;
return certified;
}
function verifyIsTime(value, validity){
var certified = dateUtil.isTime(value);
validity.typeMismatch = !certified;
return certified;
}
function verifyMinTime(value, min, instance_context, validity){
if(!min){return true;}
if(!verifyIsTime(min, validity)){
instance_context._evt.emit("error",
new TypeError('[type=time][min='+min+'] is invalid time.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxTime(value, max, instance_context, validity){
if(!max){return true;}
if(!verifyIsTime(max, validity)){
instance_context._evt.emit("error",
new TypeError('[type=time][max='+max+'] is invalid time.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
validity.rangeOverflow = !certified;
return certified;
}
function verifyIsDate(value, validity){
var certified = dateUtil.isDate(value);
if (validity){
validity.typeMismatch = !certified;
}
return certified;
}
function verifyMinDate(value, min, instance_context, validity){
if(!min){return true;}
// Do't change validity on verify `min` rule.
if(!verifyIsDate(min /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=date][min='+min+'] is invalid date.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxDate(value, max, instance_context, validity){
if(!max){return true;}
// Do't change validity on verify `max` rule.
if(!verifyIsDate(max/* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=date][max='+max+'] is invalid date.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
validity.rangeOverflow = !certified;
return certified;
}
// http://www.w3.org/TR/html-markup/input.datetime.html
function verifyIsDateTime(value, validity){
var certified = dateUtil.isDateTime(value);
if (validity) {
validity.typeMismatch = !certified;
}
return certified;
}
function verifyMinDateTime(value, min, instance_context, validity){
if(!min){return true;}
// Do't change validity on verify `min` rule.
// TODO: test cases.
if(!verifyIsDateTime(min /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=datetime][min='+min+'] is invalid datetime.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxDateTime(value, max, instance_context, validity){
if(!max){return true;}
// Do't change validity on verify `max` rule.
// TODO: test cases.
if(!verifyIsDateTime(max /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=datetime][max='+max+'] is invalid datetime.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
validity.rangeOverflow = !certified;
return certified;
}
// [input=type=datetime-local](http://www.w3.org/TR/html-markup/input.datetime-local.html)
var RE_DATETIME_LOCAL = /^\d{4,}\-\d\d\-\d\dT\d\d:\d\d:\d\d(?:[+-]\d\d:\d\d)?Z?$/;
function verifyIsDateTimeLocal(value, validity){
var certified = dateUtil.isDateTime(value);
if (validity) {
validity.typeMismatch = !certified;
}
return certified;
}
function verifyMinDateTimeLocal(value, min, instance_context, validity){
if(!min){return true;}
// Do't change validity on verify `min` rule.
// TODO: test cases.
if(!verifyIsDateTimeLocal(min /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=datetime-local][min='+min+'] is invalid datetime.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxDateTimeLocal(value, max, instance_context, validity){
if(!max){return true;}
// Do't change validity on verify `max` rule.
// TODO: test cases.
if(!verifyIsDateTimeLocal(max /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=datetime-local][max='+max+'] is invalid datetime.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
validity.rangeOverflow = !certified;
return certified;
}
var RE_WEEK = /^\d{4,}-W\d{2}$/;
function verifyIsWeek(value, validity){
var certified = dateUtil.isWeek(value);
if (validity) {
validity.typeMismatch = !certified;
}
return certified;
}
function verifyMinWeek(value, min, instance_context, validity){
if(!min){return true;}
// Do't change validity on verify `min` rule.
// TODO: test cases.
if(!verifyIsWeek(min /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=week][min='+min+'] is invalid week.'));
return true;
}
var certified = dateUtil.distanceDate(value, min) >= 0;
// XXX: Non-Effect.
validity.rangeUnderflow = !certified;
return certified;
}
function verifyMaxWeek(value, max, instance_context, validity){
if(!max){return true;}
// Do't change validity on verify `max` rule.
// TODO: test cases.
if(!verifyIsWeek(max /* , validity */)){
instance_context._evt.emit("error",
new TypeError('[type=week][max='+max+'] is invalid week.'));
return true;
}
var certified = dateUtil.distanceDate(value, max) <= 0;
// XXX: Non-Effect.
validity.rangeOverflow = !certified;
return certified;
}
// [RFC1738](http://www.faqs.org/rfcs/rfc1738.html)
var RE_URL = /^https?:\/\/(?:[\w.-]*(?::[^@]+)?@)?(?:[\w-]+\.){1,3}[\w]+(?::\d+)?(?:\/.*)?$/;
function verifyIsUrl(value, validity){
var certified = RE_URL.test(value);
validity.typeMismatch = !certified;
return certified;
}
var RE_EMAIL = /^\w+(?:[+.-]\w+)*@\w+(?:[.-]\w+)*\.\w+(?:[.-]\w+)*$/;
function verifyIsEmail(value, validity){
var certified = RE_EMAIL.test(value);
validity.typeMismatch = !certified;
return certified;
}
var RE_MOBILE = /^(?:13[0-9]|14[57]|15[0-35-9]|17[0678]|18[0-9])\d{8}$/;
function verifyIsMobile(value, validity){
var certified = RE_MOBILE.test(value);
validity.typeMismatch = !certified;
return certified;
}
var RE_TEL = /^(?:\(\+\d{2}\))?\d{3,4}\-\d{7,8}$/;
function verifyIsTel(value, validity){
var certified = RE_TEL.test(value);
validity.typeMismatch = !certified;
return certified;
}
var RE_COLOR = /^#[0-9a-fA-F]{6}$/;
function verifyIsColor(value, validity){
var certified = RE_COLOR.test(value);
validity.typeMismatch = !certified;
return certified;
}
function verifyPattern(pattern, value, instance_context, validity){
if(!isRegExp(pattern)){
if(!isString(pattern)){return true;}
try{
pattern = new RegExp(pattern);
}catch(ex){
instance_context._evt.emit("error", ex);
return true;
}
}
var certified = pattern.test(value);
validity.patternMismatch = !certified;
return certified;
}
function verifyPatternList(pattern, values, instance_context, validity){
var certified = true;
for(var i=0,l=values.length; i<l; i++){
certified = certified && verifyPattern(pattern, values[i], instance_context, validity);
}
return certified;
}
function verifyFunction(ruleFunction, value, datas){
if(!isFunction(ruleFunction)){return true;}
var build_in_rule = merge(BUILD_IN_RULE, {
data: function(key){
return datas[key];
}
});
return ruleFunction.call(build_in_rule, value);
}
var MIME_TYPE = {
"txt": "text/plain",
"htm": "text/html",
"html": "text/html",
"js": "application/javascript",
"css": "text/css",
"csv": "text/csv",
"xml": "text/xml",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"pdf": "application/pdf",
"doc": "application/msword",
"docx": "application/msword",
"zip": "application/zip",
"mp3": "audio/mpeg",
"ogg": "audio/ogg"
};
function typeByName(fileName){
var ext = fileName.split(".").slice(-1);
if(MIME_TYPE.hasOwnProperty(ext)){
return MIME_TYPE[ext];
}
}
// @param {Array} accept.
// @param {File} file.
// @return {Boolean}
function verifyFileType(file, accept, validity){
if(!isArray(accept) || !file || !file.name){return true;}
for(var i=0,l=accept.length; i<l; i++){
if(!file.type){
file.type = typeByName(file.name);
}
if(accept[i] === file.type){
return true;
}else if(endsWith(accept[i], "/*") &&
startsWith(file.type, accept[i].replace(/\*$/, "")) ){
return true;
}
}
validity.typeMismatch = true;
return false;
}
function verifyMinFileSize(file, min, validity){
if(!isNumber(min) || !isNumber(file.size)){return true;}
var certified = file.size >= min;
validity.tooShort = !certified;
return certified;
}
function verifyMaxFileSize(file, max, validity){
if(!isNumber(max) || !isNumber(file.size)){return true;}
var certified = file.size <= max;
validity.tooLong = !certified;
return certified;
}
var ValidityState = {
customError: "customError",
patternMismatch: "patternMismatch",
rangeOverflow: "rangeOverflow",
rangeUnderflow: "rangeUnderflow",
stepMismatch: "stepMismatch",
tooLong: "tooLong",
tooShort: "tooShort",
typeMismatch: "typeMismatch",
valueMissing: "valueMissing",
badInput: "badInput",
valid: "valid"
};
function verify(ruleName, rule, values, datas, instance_context){
var certified = true;
var validity = {
customError: false,
patternMismatch: false,
rangeOverflow: false,
rangeUnderflow: false,
stepMismatch: false,
tooLong: false,
tooShort: false,
typeMismatch: false,
valueMissing: false,
badInput: false,
valid: true,
validationMessage: ValidityState.valid
};
var resultRequired = verifyRequired(rule.required, values);
// fast return if required rule not match.
if("undefined" !== typeof resultRequired){
if (resultRequired === false) {
validity.valueMissing = true;
validity.valid = false;
validity.validationMessage = ValidityState.valueMissing;
}
instance_context._evt.emit(resultRequired ? "valid":"invalid", ruleName, values, validity);
return resultRequired;
}
if(isArray(values)){
certified = certified &&
verifyMinLengthList(rule.minlength, values, validity) &&
verifyMaxLengthList(rule.maxlength, values, validity) &&
verifyPatternList(rule.pattern, values, instance_context, validity);
}else{
certified = certified &&
verifyMinLength(rule.minlength, values, validity) &&
verifyMaxLength(rule.maxlength, values, validity) &&
verifyPattern(rule.pattern, values, instance_context, validity);
}
// FIXME: validity.
certified = certified &&
verifyMinLimit(rule.minlimit, values, validity) &&
verifyMaxLimit(rule.maxlimit, values, validity);
// rule: type, min, max.
switch(rule.type){
case RULE_TYPES.number:
case RULE_TYPES.range:
certified = certified &&
eachValues(verifyIsNumber, values, validity) &&
eachValues(verifyMin, values, rule.min, validity) &&
eachValues(verifyMax, values, rule.max, validity);
break;
case RULE_TYPES.date:
certified = certified &&
eachValues(verifyIsDate, values, validity) &&
eachValues(verifyMinDate, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxDate, values, rule.max, instance_context, validity);
break;
case RULE_TYPES.datetime:
certified = certified &&
eachValues(verifyIsDateTime, values, validity) &&
eachValues(verifyMinDateTime, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxDateTime, values, rule.max, instance_context, validity);
break;
case RULE_TYPES["datetime-local"]:
certified = certified &&
eachValues(verifyIsDateTimeLocal, values, validity) &&
eachValues(verifyMinDateTimeLocal, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxDateTimeLocal, values, rule.max, instance_context, validity);
break;
case RULE_TYPES.time:
certified = certified &&
eachValues(verifyIsTime, values, validity) &&
eachValues(verifyMinTime, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxTime, values, rule.max, instance_context, validity);
break;
case RULE_TYPES.week:
certified = certified &&
eachValues(verifyIsWeek, values, validity) &&
eachValues(verifyMinWeek, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxWeek, values, rule.max, instance_context, validity);
break;
case RULE_TYPES.month:
certified = certified &&
eachValues(verifyIsMonth, values, validity) &&
eachValues(verifyMinMonth, values, rule.min, instance_context, validity) &&
eachValues(verifyMaxMonth, values, rule.max, instance_context, validity);
break;
case RULE_TYPES.url:
certified = certified && eachValues(verifyIsUrl, values, validity);
break;
case RULE_TYPES.email:
certified = certified && eachValues(verifyIsEmail, values, validity);
break;
case RULE_TYPES.tel:
certified = certified && (
eachValues(verifyIsTel, values, validity) ||
eachValues(verifyIsMobile, values, validity)
);
break;
case RULE_TYPES.color:
certified = certified && eachValues(verifyIsColor, values, validity);
break;
case RULE_TYPES.file:
certified = certified &&
eachValues(verifyFileType, values, rule.accept, validity) &&
eachValues(verifyMinFileSize, values, rule.min, validity) &&
eachValues(verifyMaxFileSize, values, rule.max, validity);
break;
//case RULE_TYPES.select-one:
//case RULE_TYPES.radio:
//case RULE_TYPES.text:
//case RULE_TYPES.search:
//case RULE_TYPES.textarea:
//case RULE_TYPES.checkbox:
//case RULE_TYPES["select-multiple"]:
//case RULE_TYPES.password:
//default:
//break;
}
return UniPromise(
//! NOTE: Do't each loop values by verifyFunction,
// each loop values in user custom function if need.
verifyFunction(rule.custom, values, datas),
function(result){
if (!result) {
validity.customError = true;
validity.valid = false;
}
for(var key in validity){
if (validity.hasOwnProperty(key) && key !== "valid" && isBoolean(validity[key]) && validity[key]) {
validity.validationMessage = ValidityState[key];
validity.valid = false;
}
}
certified = certified && result;
validity.valid = certified;
instance_context._evt.emit(certified ? "valid":"invalid", ruleName, values, validity);
return certified;
}, function(reason){
return (reason);
}
)
}
var Validator = function(rules){
this._rules = rules;
this._evt = new Events();
};
Validator.prototype.validate = function(data){
var ME = this;
return new Promise(function(resolve, reject) {
var certified = true;
var pending = 0;
eachRules(ME._rules, function(ruleName, rule){
var values = data[ruleName];
pending ++;
UniPromise(
verify(ruleName, rule, values, data, ME),
function resolved(certify){
certified = certified && certify;
if((--pending) === 0){
ME._evt.emit("complete", certified);
resolve(certified)
}
},
function(){
ME._evt.emit("error", ruleName, rule, values, data)
pending --;
})
},
function(){
ME._evt.emit("complete", true)
resolve(true)
}
);
});
};
Validator.prototype.on = function(eventName, handler){
this._evt.on(eventName, handler);
return this;
};
Validator.prototype.off = function(eventName, handler){
if(isFunction(handler)){
this._evt.removeListener(eventName, handler);
}else{
this._evt.removeAllListeners(eventName);
}
return this;
};
Validator.rule = function(ruleName, validation){
if(!isString(ruleName)){return false;}
if(!isFunction(validation)){return BUILD_IN_RULE[ruleName];}
BUILD_IN_RULE[ruleName] = validation;
return true;
};
module.exports = Validator;