• R/O
  • SSH

micro-check: Commit

input validation library


Commit MetaInfo

Revision9b847380d77a197bbf44c604eb98d2af0b9632cc (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 65f07fa62db1 -r 9b847380d77a src/microcheck.js
--- a/src/microcheck.js Thu Apr 12 19:12:01 2018 +0900
+++ b/src/microcheck.js Mon Apr 17 13:18:14 2017 +0900
@@ -84,6 +84,90 @@
8484 var __options,
8585 __object;
8686
87+ var __utility = {
88+ isFloat :function(val){
89+ // parseFloat("1.2345X") にも対応する
90+
91+ var floatRegex = /^-?\d*(?:[.,]\d*?)?$/;
92+ var isSimpleFloat = floatRegex.test(val);
93+ var expRegex = /^-?\d*(?:[.,]\d*?)?e[+-]?\d*$/;
94+ var isExponentFloat = expRegex.test(val);
95+
96+ if (!isSimpleFloat && !isExponentFloat)
97+ return false;
98+
99+ return !isNaN(parseFloat(val));
100+ },
101+
102+ isInt :function(val){
103+ // parseInt("123ACDAYO") にも対応する
104+
105+ if (__utility.isFloat(val))
106+ val = parseFloat(val);
107+ else
108+ return false;
109+
110+ var intRegex = /^-?\d+$/;
111+ var isSimpleInt = intRegex.test(val);
112+
113+ var exponentRegex = /^-?\d+e+?\d+$/;
114+ var isExponentInt = exponentRegex.test(val);
115+
116+ if (!isSimpleInt && !isExponentInt)
117+ return false;
118+
119+ return !isNaN(parseInt(val));
120+ },
121+
122+ getDecimalCount :function(f){
123+ if(!isFloat(f))
124+ return NaN;
125+
126+ var fVal = parseFloat(f);
127+ var decimal = fVal.toString().split(".");
128+
129+ return fVal % 1 === 0 ?
130+ 0 : (
131+ decimal.length !== 2 ?
132+ 0 :
133+ decimal[1].length
134+ );
135+
136+ },
137+
138+ noScientific :function(n){
139+
140+ var data = (n + "").split(/[eE]/);
141+
142+ if(data.length === 1)
143+ return data[0];
144+
145+ var z = '',
146+ sign = n < 0 ? '-' : '',
147+ coefficient = data[0].replace('.', ''),
148+ exponent = parseInt(data[1]) + 1,
149+ result;
150+
151+ if(exponent < 0){
152+ z = sign + '0.';
153+ while(exponent++)
154+ z += '0';
155+
156+ result = z + coefficient.replace(/^\-/,'');
157+ return result;
158+ } else {
159+ exponent -= coefficient.length;
160+
161+ while(exponent--)
162+ z += '0';
163+
164+ result = coefficient + z;
165+ return result;
166+ }
167+ },
168+ };
169+
170+
87171 var __ruleNames = [
88172 //@see {Form_validation.php}
89173 //@see {http://datamapper.maglok.nl/pages/validation.html}
@@ -105,11 +189,10 @@
105189 //"alpha_dash_dot",
106190 //"alpha_slash_dot",
107191 //"alpha_dash",
108- "integer",
109192 "numeric",
110- //"boolean",
193+ "boolean",
111194 //"is_numeric",
112- //"integer",
195+ "integer",
113196 //"decimal",
114197 //"greater_than",
115198 //"less_than",
@@ -251,7 +334,7 @@
251334
252335 integer :function(a, b){
253336 var getMessage = MicroCheck.message.numeric;
254-
337+/*
255338 var isInteger = new RegExp("^[\-+]?[0-9]*$").test(a) &&
256339 (a + "").split(".").length <= 2;
257340
@@ -262,12 +345,18 @@
262345 !isInteger ?
263346 false :
264347 getMessage.call(this, b);
348+*/
265349
350+ return b ?
351+ __utility.isInt(a) ?
352+ false :
353+ getMessage.call(this) :
354+ false;
266355 },
267356
268357 numeric :function(a, b){
269358 var getMessage = MicroCheck.message.numeric;
270-
359+/*
271360 var isNumeric = new RegExp("^[\-+]?[0-9]*(\.[0-9]*)?$").test(a) &&
272361 (a + "").split(".").length <= 2;
273362
@@ -278,6 +367,33 @@
278367 !isNumeric ?
279368 false :
280369 getMessage.call(this, b);
370+*/
371+
372+ ///// this was the old check algorithm
373+ // var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
374+ // (a + "").split(".").length <= 2;
375+
376+ return b ?
377+ __utility.isFloat(a) ?
378+ false :
379+ getMessage.call(this) :
380+ false;
381+
382+ },
383+
384+ boolean :function(a, b){
385+ var getMessage = MicroCheck.message.boolean;
386+
387+ var isBoolean = typeof a === typeof true;
388+
389+ return b ?
390+ isBoolean ?
391+ false :
392+ getMessage.call(this) :
393+ false;
394+
395+ },
396+
281397
282398 },
283399
@@ -641,6 +757,12 @@
641757 '"' + this.value + '" is not a number' :
642758 '"' + this.value + '" is a number';
643759 },
760+ boolean :function(b){
761+ "use strict";
762+
763+ return '"' + this.value + '" type is not boolean';
764+ },
765+ },
644766 single_byte :function(){
645767 return '"' + this.value + '" contains unicode characters';
646768 },
diff -r 65f07fa62db1 -r 9b847380d77a test/microcheck.test.html
--- a/test/microcheck.test.html Thu Apr 12 19:12:01 2018 +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 65f07fa62db1 -r 9b847380d77a test/microcheck.test.js
--- a/test/microcheck.test.js Thu Apr 12 19:12:01 2018 +0900
+++ b/test/microcheck.test.js Mon Apr 17 13:18:14 2017 +0900
@@ -278,6 +278,38 @@
278278 name :undefined,
279279 },
280280 },
281+ types :[
282+ {
283+ string :"true",
284+ boolean :true,
285+ int :1,
286+ value :true,
287+ },
288+ {
289+ string :"false",
290+ boolean :false,
291+ int :0,
292+ value :false,
293+ },
294+ {
295+ string :"undefined",
296+ boolean :undefined,
297+ int :-1,
298+ value :undefined,
299+ },
300+ {
301+ string :"null",
302+ boolean :null,
303+ int :0,
304+ value :null,
305+ },
306+ {
307+ string :"float",
308+ boolean :1,
309+ int :1,
310+ value :1.234,
311+ },
312+ ],
281313 },
282314
283315 /* */// ------------------------------------------------ //
@@ -430,9 +462,10 @@
430462
431463 assertEqual(2, result.length);
432464
433- result.map(function(r){
434- log(r);
435- });
465+ if (settings.verbose)
466+ result.map(function(r){
467+ log(r);
468+ });
436469
437470 },
438471
@@ -478,9 +511,10 @@
478511
479512 assertEqual(1, result.length);
480513
481- result.map(function(r){
482- log(r);
483- });
514+ if (settings.verbose)
515+ result.map(function(r){
516+ log(r);
517+ });
484518
485519 },
486520
@@ -526,9 +560,10 @@
526560
527561 assertEqual(2, result.length);
528562
529- result.map(function(r){
530- log(r);
531- });
563+ if (settings.verbose)
564+ result.map(function(r){
565+ log(r);
566+ });
532567
533568 },
534569
@@ -576,9 +611,10 @@
576611 // there are 3 companies with info.duration
577612 assertEqual(1, result.length);
578613
579- result.map(function(r){
580- log(r);
581- });
614+ if (settings.verbose)
615+ result.map(function(r){
616+ log(r);
617+ });
582618
583619 },
584620
@@ -626,9 +662,10 @@
626662 // companies with names longer than 3
627663 assertEqual(this.data.companies.length, result.length);
628664
629- result.map(function(r){
630- log(r);
631- });
665+ if (settings.verbose)
666+ result.map(function(r){
667+ log(r);
668+ });
632669
633670 },
634671
@@ -675,9 +712,10 @@
675712
676713 assertEqual(this.data.companies.length, result.length);
677714
678- result.map(function(r){
679- log(r);
680- });
715+ if (settings.verbose)
716+ result.map(function(r){
717+ log(r);
718+ });
681719
682720 },
683721
@@ -724,9 +762,10 @@
724762
725763 assertEqual(1, result.length);
726764
727- result.map(function(r){
728- log(r);
729- });
765+ if (settings.verbose)
766+ result.map(function(r){
767+ log(r);
768+ });
730769
731770 },
732771
@@ -796,32 +835,10 @@
796835
797836 assertEqual(1, result.length);
798837
799- result.map(function(r){
800- log(r);
801- });
802-
803- },
804-
805- test_integer :function(){
806- "use strict";
807-
808- var validationRules = [
809- [
810- "company id",
811- "companies[*].id",
812- {
813- integer :true,
814- },
815- ],
816- ];
817-
818- var cm = new MicroCheck(validationRules)
819- .setData(this.data);
820-
821- var result = cm.validate();
822-
823- // all test passed
824- assertEqual(0, result.length);
838+ if (settings.verbose)
839+ result.map(function(r){
840+ log(r);
841+ });
825842
826843 },
827844
@@ -855,6 +872,51 @@
855872
856873 },
857874
875+ test_numeric_representations :function(){
876+ "use strict";
877+
878+ var testObject = {
879+ values :[
880+ 1,
881+ 0,
882+ 0.0,
883+ .0,
884+ .1,
885+ 1.23e4,
886+ 1.23e+4,
887+ 1.23e-6,
888+ "1",
889+ "0",
890+ "00",
891+ "0.0",
892+ ".0",
893+ ".1",
894+ "1.23e+4",
895+ "1.23e+4",
896+ "1.23e-6",
897+ ]
898+ };
899+
900+ var validationRules = [
901+ [
902+ "number representations",
903+ "values[*]",
904+ {
905+ numeric :true,
906+ },
907+ ],
908+ ];
909+
910+ var cm = new MicroCheck(validationRules)
911+ .setData(testObject);
912+
913+ var result = cm.validate();
914+
915+ // all test passed
916+ assertEqual(0, result.length);
917+
918+ },
919+
858920 test_numeric_fail :function(){
859921 "use strict";
860922
@@ -887,12 +949,200 @@
887949 // all test passed
888950 assertEqual(projCount, result.length);
889951
890- result.map(function(r){
891- log(r);
892- });
952+ if (settings.verbose)
953+ result.map(function(r){
954+ log(r);
955+ });
893956
894957 },
895958
959+ test_boolean :function(){
960+ "use strict";
961+
962+ var validationRules = [
963+ [
964+ "types",
965+ "types[0].boolean",
966+ {
967+ boolean :true,
968+ },
969+ ],
970+ ];
971+
972+ var cm = new MicroCheck(validationRules)
973+ .setData(this.data);
974+
975+ var result = cm.validate();
976+
977+ // all test passed
978+ assertEqual(0, result.length);
979+ },
980+
981+ test_boolean_fail :function(){
982+ "use strict";
983+
984+ var validationRules = [
985+ [
986+ "types",
987+ "types[1].string",
988+ {
989+ boolean :true,
990+ },
991+ ],
992+ ];
993+
994+ var cm = new MicroCheck(validationRules)
995+ .setData(this.data);
996+
997+ var result = cm.validate();
998+
999+ assertEqual(1, result.length);
1000+
1001+ if (settings.verbose)
1002+ result.map(function(r){
1003+ log(r);
1004+ });
1005+ },
1006+
1007+ test_integer :function(){
1008+ "use strict";
1009+
1010+ var validationRules = [
1011+ [
1012+ "types",
1013+ "types[4].int",
1014+ {
1015+ integer :true,
1016+ },
1017+ ],
1018+ ];
1019+
1020+ var cm = new MicroCheck(validationRules)
1021+ .setData(this.data);
1022+
1023+ var result = cm.validate();
1024+
1025+ // all test passed
1026+ assertEqual(0, result.length);
1027+
1028+ },
1029+
1030+ test_integer_representations :function(){
1031+ "use strict";
1032+
1033+ var testObject = {
1034+ values :[
1035+ 0,
1036+ 1,
1037+ 0.0,
1038+ .0,
1039+ 1.2e3,
1040+ "0",
1041+ "00",
1042+ "0.0",
1043+ "1",
1044+ ".0",
1045+ "1.2e3",
1046+ ]
1047+ };
1048+
1049+ var validationRules = [
1050+ [
1051+ "integer representations",
1052+ "values[*]",
1053+ {
1054+ integer :true,
1055+ },
1056+ ],
1057+ ];
1058+
1059+ var cm = new MicroCheck(validationRules)
1060+ .setData(testObject);
1061+
1062+ var result = cm.validate();
1063+
1064+ // all test passed
1065+ assertEqual(0, result.length);
1066+
1067+ },
1068+
1069+ test_integer_fail :function(){
1070+ "use strict";
1071+
1072+ var validationRules = [
1073+ [
1074+ "types",
1075+ "types[4].value",
1076+ {
1077+ integer :true,
1078+ },
1079+ ],
1080+ ];
1081+
1082+ var cm = new MicroCheck(validationRules)
1083+ .setData(this.data);
1084+
1085+ var result = cm.validate();
1086+
1087+ // all test passed
1088+ assertEqual(1, result.length);
1089+
1090+ if (settings.verbose)
1091+ result.map(function(r){
1092+ log(r);
1093+ });
1094+ },
1095+
1096+ test_integer_representations_fail :function(){
1097+ "use strict";
1098+
1099+ var testObject = {
1100+ values :[
1101+ true,
1102+ false,
1103+ NaN,
1104+ //// required rule must be set or else undefined and null will be ignored
1105+ //undefined,
1106+ //null,
1107+ 1.2345,
1108+ 1.2345e+3,
1109+ 3.43-2,
1110+ 0.001,
1111+ .1,
1112+ .123,
1113+ "1.2345",
1114+ "1.2345e+3",
1115+ "3.43-2",
1116+ "0.001",
1117+ ".123",
1118+ ".1",
1119+ ],
1120+ };
1121+
1122+ var validationRules = [
1123+ [
1124+ "float representations",
1125+ "values[*]",
1126+ {
1127+ integer :true,
1128+ },
1129+ ],
1130+ ];
1131+
1132+ var cm = new MicroCheck(validationRules)
1133+ .setData(testObject);
1134+
1135+ var result = cm.validate();
1136+
1137+ // all failed
1138+ assertEqual(testObject.values.length, result.length);
1139+
1140+ if (settings.verbose)
1141+ result.map(function(r){
1142+ log(r);
1143+ });
1144+ },
1145+
8961146 test_ignore_numeric :function(){
8971147 "use strict";
8981148
@@ -916,7 +1166,7 @@
9161166
9171167 },
9181168
919- test_not_numeric_fail :function(){
1169+ test_ignore_numeric :function(){
9201170 "use strict";
9211171
9221172 var validationRules = [
@@ -935,11 +1185,12 @@
9351185 var result = cm.validate();
9361186
9371187 // all test passed
938- assertEqual(this.data.companies.length, result.length);
1188+ assertEqual(0, result.length);
9391189
940- result.map(function(r){
941- log(r);
942- });
1190+ if (settings.verbose)
1191+ result.map(function(r){
1192+ log(r);
1193+ });
9431194
9441195 },
9451196
@@ -985,6 +1236,11 @@
9851236
9861237 // only one company code does not match the expression
9871238 assertEqual(1, result.length);
1239+
1240+ if (settings.verbose)
1241+ result.map(function(r){
1242+ log(r);
1243+ });
9881244 },
9891245
9901246 test_alpha :function(){
@@ -1052,6 +1308,11 @@
10521308
10531309 // company codes have number
10541310 assertEqual(this.data.companies.length, result.length);
1311+
1312+ if (settings.verbose)
1313+ result.map(function(r){
1314+ log(r);
1315+ });
10551316 },
10561317
10571318 test_alpha_numeric :function() {
@@ -1118,6 +1379,11 @@
11181379
11191380 // axas has a '-' in its code
11201381 assertEqual(1, result.length);
1382+
1383+ if (settings.verbose)
1384+ result.map(function(r){
1385+ log(r);
1386+ });
11211387 },
11221388
11231389 test_single_byte :function(){
@@ -1185,6 +1451,11 @@
11851451
11861452 // all kds members have japanese names
11871453 assertEqual(this.data.companies[0].info.members.length, result.length);
1454+
1455+ if (settings.verbose)
1456+ result.map(function(r){
1457+ log(r);
1458+ });
11881459 },
11891460
11901461 test_single_byte_fail2 :function(){
@@ -1226,6 +1497,11 @@
12261497 var result = cm.validate();
12271498
12281499 assertEqual(3, result.length);
1500+
1501+ if (settings.verbose)
1502+ result.map(function(r){
1503+ log(r);
1504+ });
12291505 },
12301506
12311507 /* */// ------------------------------------------------ //
Show on old repository browser