• R/O
  • SSH

micro-check: Commit

input validation library


Commit MetaInfo

Revision72364efb306eccff30a88f9e3830227d6028092d (tree)
Time2017-04-17 13:18:14
Authorfrostbane <frostbane@prog...>
Commiterfrostbane

Log Message

add boolean rule

fixed check for numeric rule (float/double) and integer rule

Change Summary

Incremental Difference

diff -r c99827c217cb -r 72364efb306e src/microcheck.js
--- a/src/microcheck.js Fri Apr 14 14:55:28 2017 +0900
+++ b/src/microcheck.js Mon Apr 17 13:18:14 2017 +0900
@@ -48,9 +48,7 @@
4848
4949 // todo fix globals
5050 /* global
51-isInt,
5251 isFloat,
53-format,
5452 getDecimalCount,
5553 trim
5654 */
@@ -81,6 +79,90 @@
8179 var __options,
8280 __object;
8381
82+ var __utility = {
83+ isFloat :function(val){
84+ // parseFloat("1.2345X") にも対応する
85+
86+ var floatRegex = /^-?\d*(?:[.,]\d*?)?$/;
87+ var isSimpleFloat = floatRegex.test(val);
88+ var expRegex = /^-?\d*(?:[.,]\d*?)?e[+-]?\d*$/;
89+ var isExponentFloat = expRegex.test(val);
90+
91+ if (!isSimpleFloat && !isExponentFloat)
92+ return false;
93+
94+ return !isNaN(parseFloat(val));
95+ },
96+
97+ isInt :function(val){
98+ // parseInt("123ACDAYO") にも対応する
99+
100+ if (__utility.isFloat(val))
101+ val = parseFloat(val);
102+ else
103+ return false;
104+
105+ var intRegex = /^-?\d+$/;
106+ var isSimpleInt = intRegex.test(val);
107+
108+ var exponentRegex = /^-?\d+e+?\d+$/;
109+ var isExponentInt = exponentRegex.test(val);
110+
111+ if (!isSimpleInt && !isExponentInt)
112+ return false;
113+
114+ return !isNaN(parseInt(val));
115+ },
116+
117+ getDecimalCount :function(f){
118+ if(!isFloat(f))
119+ return NaN;
120+
121+ var fVal = parseFloat(f);
122+ var decimal = fVal.toString().split(".");
123+
124+ return fVal % 1 === 0 ?
125+ 0 : (
126+ decimal.length !== 2 ?
127+ 0 :
128+ decimal[1].length
129+ );
130+
131+ },
132+
133+ noScientific :function(n){
134+
135+ var data = (n + "").split(/[eE]/);
136+
137+ if(data.length === 1)
138+ return data[0];
139+
140+ var z = '',
141+ sign = n < 0 ? '-' : '',
142+ coefficient = data[0].replace('.', ''),
143+ exponent = parseInt(data[1]) + 1,
144+ result;
145+
146+ if(exponent < 0){
147+ z = sign + '0.';
148+ while(exponent++)
149+ z += '0';
150+
151+ result = z + coefficient.replace(/^\-/,'');
152+ return result;
153+ } else {
154+ exponent -= coefficient.length;
155+
156+ while(exponent--)
157+ z += '0';
158+
159+ result = coefficient + z;
160+ return result;
161+ }
162+ },
163+ };
164+
165+
84166 var __ruleNames = [
85167 //@see {Form_validation.php}
86168 //@see {http://datamapper.maglok.nl/pages/validation.html}
@@ -103,9 +185,9 @@
103185 //"alpha_slash_dot",
104186 //"alpha_dash",
105187 "numeric",
106- //"boolean",
188+ "boolean",
107189 //"is_numeric",
108- //"integer",
190+ "integer",
109191 //"decimal",
110192 //"greater_than",
111193 //"less_than",
@@ -215,8 +297,8 @@
215297
216298 return b ?
217299 isValid ?
218- false :
219- getMessage.call(this) :
300+ false :
301+ getMessage.call(this) :
220302 false;
221303 },
222304
@@ -248,16 +330,39 @@
248330 numeric :function(a, b){
249331 var getMessage = MicroCheck.message.numeric;
250332
251- var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
252- (a + "").split(".").length <= 2;
333+ ///// this was the old check algorithm
334+ // var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
335+ // (a + "").split(".").length <= 2;
253336
254337 return b ?
255- isNumeric ?
256- false :
257- getMessage.call(this, b) :
258- !isNumeric ?
259- false :
260- getMessage.call(this, b);
338+ __utility.isFloat(a) ?
339+ false :
340+ getMessage.call(this) :
341+ false;
342+
343+ },
344+
345+ boolean :function(a, b){
346+ var getMessage = MicroCheck.message.boolean;
347+
348+ var isBoolean = typeof a === typeof true;
349+
350+ return b ?
351+ isBoolean ?
352+ false :
353+ getMessage.call(this) :
354+ false;
355+
356+ },
357+
358+ integer :function(a, b){
359+ var getMessage = MicroCheck.message.boolean;
360+
361+ return b ?
362+ __utility.isInt(a) ?
363+ false :
364+ getMessage.call(this) :
365+ false;
261366
262367 },
263368
@@ -636,12 +741,20 @@
636741
637742 return '"' + this.value + '" contains non-alphanumeric characters';
638743 },
639- numeric :function(b){
744+ numeric :function(){
640745 "use strict";
641746
642- return b ?
643- '"' + this.value + '" is not a number' :
644- '"' + this.value + '" is a number';
747+ return '"' + this.value + '" is not a number';
748+ },
749+ boolean :function(b){
750+ "use strict";
751+
752+ return '"' + this.value + '" type is not boolean';
753+ },
754+ boolean :function(b){
755+ "use strict";
756+
757+ return '"' + this.value + '" is not an integer';
645758 },
646759 single_byte :function(){
647760 return '"' + this.value + '" contains unicode characters';
diff -r c99827c217cb -r 72364efb306e test/microcheck.test.html
--- a/test/microcheck.test.html Fri Apr 14 14:55:28 2017 +0900
+++ b/test/microcheck.test.html Mon Apr 17 13:18:14 2017 +0900
@@ -17,7 +17,7 @@
1717 clearOnStart :true,
1818 stopOnFail :false,
1919 stopOnException :true,
20- verbose :true,
20+ verbose :false,
2121 autoStart :false,
2222 outputColor :true,
2323 debugMode :false,
diff -r c99827c217cb -r 72364efb306e test/microcheck.test.js
--- a/test/microcheck.test.js Fri Apr 14 14:55:28 2017 +0900
+++ b/test/microcheck.test.js Mon Apr 17 13:18:14 2017 +0900
@@ -275,6 +275,38 @@
275275 name :undefined,
276276 },
277277 },
278+ types :[
279+ {
280+ string :"true",
281+ boolean :true,
282+ int :1,
283+ value :true,
284+ },
285+ {
286+ string :"false",
287+ boolean :false,
288+ int :0,
289+ value :false,
290+ },
291+ {
292+ string :"undefined",
293+ boolean :undefined,
294+ int :-1,
295+ value :undefined,
296+ },
297+ {
298+ string :"null",
299+ boolean :null,
300+ int :0,
301+ value :null,
302+ },
303+ {
304+ string :"float",
305+ boolean :1,
306+ int :1,
307+ value :1.234,
308+ },
309+ ],
278310 },
279311
280312 /* */// ------------------------------------------------ //
@@ -427,9 +459,10 @@
427459
428460 assertEqual(2, result.length);
429461
430- result.map(function(r){
431- log(r);
432- });
462+ if (settings.verbose)
463+ result.map(function(r){
464+ log(r);
465+ });
433466
434467 },
435468
@@ -475,9 +508,10 @@
475508
476509 assertEqual(1, result.length);
477510
478- result.map(function(r){
479- log(r);
480- });
511+ if (settings.verbose)
512+ result.map(function(r){
513+ log(r);
514+ });
481515
482516 },
483517
@@ -523,9 +557,10 @@
523557
524558 assertEqual(2, result.length);
525559
526- result.map(function(r){
527- log(r);
528- });
560+ if (settings.verbose)
561+ result.map(function(r){
562+ log(r);
563+ });
529564
530565 },
531566
@@ -573,9 +608,10 @@
573608 // there are 3 companies with info.duration
574609 assertEqual(1, result.length);
575610
576- result.map(function(r){
577- log(r);
578- });
611+ if (settings.verbose)
612+ result.map(function(r){
613+ log(r);
614+ });
579615
580616 },
581617
@@ -623,9 +659,10 @@
623659 // companies with names longer than 3
624660 assertEqual(this.data.companies.length, result.length);
625661
626- result.map(function(r){
627- log(r);
628- });
662+ if (settings.verbose)
663+ result.map(function(r){
664+ log(r);
665+ });
629666
630667 },
631668
@@ -672,9 +709,10 @@
672709
673710 assertEqual(this.data.companies.length, result.length);
674711
675- result.map(function(r){
676- log(r);
677- });
712+ if (settings.verbose)
713+ result.map(function(r){
714+ log(r);
715+ });
678716
679717 },
680718
@@ -721,9 +759,10 @@
721759
722760 assertEqual(1, result.length);
723761
724- result.map(function(r){
725- log(r);
726- });
762+ if (settings.verbose)
763+ result.map(function(r){
764+ log(r);
765+ });
727766
728767 },
729768
@@ -793,9 +832,10 @@
793832
794833 assertEqual(1, result.length);
795834
796- result.map(function(r){
797- log(r);
798- });
835+ if (settings.verbose)
836+ result.map(function(r){
837+ log(r);
838+ });
799839
800840 },
801841
@@ -822,6 +862,51 @@
822862
823863 },
824864
865+ test_numeric_representations :function(){
866+ "use strict";
867+
868+ var testObject = {
869+ values :[
870+ 1,
871+ 0,
872+ 0.0,
873+ .0,
874+ .1,
875+ 1.23e4,
876+ 1.23e+4,
877+ 1.23e-6,
878+ "1",
879+ "0",
880+ "00",
881+ "0.0",
882+ ".0",
883+ ".1",
884+ "1.23e+4",
885+ "1.23e+4",
886+ "1.23e-6",
887+ ]
888+ };
889+
890+ var validationRules = [
891+ [
892+ "number representations",
893+ "values[*]",
894+ {
895+ numeric :true,
896+ },
897+ ],
898+ ];
899+
900+ var cm = new MicroCheck(validationRules)
901+ .setData(testObject);
902+
903+ var result = cm.validate();
904+
905+ // all test passed
906+ assertEqual(0, result.length);
907+
908+ },
909+
825910 test_numeric_fail :function(){
826911 "use strict";
827912
@@ -854,12 +939,200 @@
854939 // all test passed
855940 assertEqual(projCount, result.length);
856941
857- result.map(function(r){
858- log(r);
859- });
942+ if (settings.verbose)
943+ result.map(function(r){
944+ log(r);
945+ });
860946
861947 },
862948
949+ test_boolean :function(){
950+ "use strict";
951+
952+ var validationRules = [
953+ [
954+ "types",
955+ "types[0].boolean",
956+ {
957+ boolean :true,
958+ },
959+ ],
960+ ];
961+
962+ var cm = new MicroCheck(validationRules)
963+ .setData(this.data);
964+
965+ var result = cm.validate();
966+
967+ // all test passed
968+ assertEqual(0, result.length);
969+ },
970+
971+ test_boolean_fail :function(){
972+ "use strict";
973+
974+ var validationRules = [
975+ [
976+ "types",
977+ "types[1].string",
978+ {
979+ boolean :true,
980+ },
981+ ],
982+ ];
983+
984+ var cm = new MicroCheck(validationRules)
985+ .setData(this.data);
986+
987+ var result = cm.validate();
988+
989+ assertEqual(1, result.length);
990+
991+ if (settings.verbose)
992+ result.map(function(r){
993+ log(r);
994+ });
995+ },
996+
997+ test_integer :function(){
998+ "use strict";
999+
1000+ var validationRules = [
1001+ [
1002+ "types",
1003+ "types[4].int",
1004+ {
1005+ integer :true,
1006+ },
1007+ ],
1008+ ];
1009+
1010+ var cm = new MicroCheck(validationRules)
1011+ .setData(this.data);
1012+
1013+ var result = cm.validate();
1014+
1015+ // all test passed
1016+ assertEqual(0, result.length);
1017+
1018+ },
1019+
1020+ test_integer_representations :function(){
1021+ "use strict";
1022+
1023+ var testObject = {
1024+ values :[
1025+ 0,
1026+ 1,
1027+ 0.0,
1028+ .0,
1029+ 1.2e3,
1030+ "0",
1031+ "00",
1032+ "0.0",
1033+ "1",
1034+ ".0",
1035+ "1.2e3",
1036+ ]
1037+ };
1038+
1039+ var validationRules = [
1040+ [
1041+ "integer representations",
1042+ "values[*]",
1043+ {
1044+ integer :true,
1045+ },
1046+ ],
1047+ ];
1048+
1049+ var cm = new MicroCheck(validationRules)
1050+ .setData(testObject);
1051+
1052+ var result = cm.validate();
1053+
1054+ // all test passed
1055+ assertEqual(0, result.length);
1056+
1057+ },
1058+
1059+ test_integer_fail :function(){
1060+ "use strict";
1061+
1062+ var validationRules = [
1063+ [
1064+ "types",
1065+ "types[4].value",
1066+ {
1067+ integer :true,
1068+ },
1069+ ],
1070+ ];
1071+
1072+ var cm = new MicroCheck(validationRules)
1073+ .setData(this.data);
1074+
1075+ var result = cm.validate();
1076+
1077+ // all test passed
1078+ assertEqual(1, result.length);
1079+
1080+ if (settings.verbose)
1081+ result.map(function(r){
1082+ log(r);
1083+ });
1084+ },
1085+
1086+ test_integer_representations_fail :function(){
1087+ "use strict";
1088+
1089+ var testObject = {
1090+ values :[
1091+ true,
1092+ false,
1093+ NaN,
1094+ //// required rule must be set or else undefined and null will be ignored
1095+ //undefined,
1096+ //null,
1097+ 1.2345,
1098+ 1.2345e+3,
1099+ 3.43-2,
1100+ 0.001,
1101+ .1,
1102+ .123,
1103+ "1.2345",
1104+ "1.2345e+3",
1105+ "3.43-2",
1106+ "0.001",
1107+ ".123",
1108+ ".1",
1109+ ],
1110+ };
1111+
1112+ var validationRules = [
1113+ [
1114+ "float representations",
1115+ "values[*]",
1116+ {
1117+ integer :true,
1118+ },
1119+ ],
1120+ ];
1121+
1122+ var cm = new MicroCheck(validationRules)
1123+ .setData(testObject);
1124+
1125+ var result = cm.validate();
1126+
1127+ // all failed
1128+ assertEqual(testObject.values.length, result.length);
1129+
1130+ if (settings.verbose)
1131+ result.map(function(r){
1132+ log(r);
1133+ });
1134+ },
1135+
8631136 test_ignore_numeric :function(){
8641137 "use strict";
8651138
@@ -883,7 +1156,7 @@
8831156
8841157 },
8851158
886- test_not_numeric_fail :function(){
1159+ test_ignore_numeric :function(){
8871160 "use strict";
8881161
8891162 var validationRules = [
@@ -902,11 +1175,12 @@
9021175 var result = cm.validate();
9031176
9041177 // all test passed
905- assertEqual(this.data.companies.length, result.length);
1178+ assertEqual(0, result.length);
9061179
907- result.map(function(r){
908- log(r);
909- });
1180+ if (settings.verbose)
1181+ result.map(function(r){
1182+ log(r);
1183+ });
9101184
9111185 },
9121186
@@ -952,6 +1226,11 @@
9521226
9531227 // only one company code does not match the expression
9541228 assertEqual(1, result.length);
1229+
1230+ if (settings.verbose)
1231+ result.map(function(r){
1232+ log(r);
1233+ });
9551234 },
9561235
9571236 test_alpha :function(){
@@ -1019,6 +1298,11 @@
10191298
10201299 // company codes have number
10211300 assertEqual(this.data.companies.length, result.length);
1301+
1302+ if (settings.verbose)
1303+ result.map(function(r){
1304+ log(r);
1305+ });
10221306 },
10231307
10241308 test_alpha_numeric :function() {
@@ -1085,6 +1369,11 @@
10851369
10861370 // axas has a '-' in its code
10871371 assertEqual(1, result.length);
1372+
1373+ if (settings.verbose)
1374+ result.map(function(r){
1375+ log(r);
1376+ });
10881377 },
10891378
10901379 test_single_byte :function(){
@@ -1152,6 +1441,11 @@
11521441
11531442 // all kds members have japanese names
11541443 assertEqual(this.data.companies[0].info.members.length, result.length);
1444+
1445+ if (settings.verbose)
1446+ result.map(function(r){
1447+ log(r);
1448+ });
11551449 },
11561450
11571451 test_single_byte_fail2 :function(){
@@ -1193,6 +1487,11 @@
11931487 var result = cm.validate();
11941488
11951489 assertEqual(3, result.length);
1490+
1491+ if (settings.verbose)
1492+ result.map(function(r){
1493+ log(r);
1494+ });
11961495 },
11971496
11981497 /* */// ------------------------------------------------ //
Show on old repository browser