input validation library
Revisión | b62f0076534aeb6bc385ee5a4756b07d1ceed5fe (tree) |
---|---|
Tiempo | 2018-04-12 19:11:40 |
Autor | frostbane <frostbane@prog...> |
Commiter | frostbane |
fix numeric check bug
numeric now allows numbers with decimals
add integer check
@@ -1,6 +1,6 @@ | ||
1 | 1 | { |
2 | 2 | "name" : "micro-check", |
3 | - "version" : "v0.0.2", | |
3 | + "version" : "v0.0.3", | |
4 | 4 | "description" : "Object property validation", |
5 | 5 | "repository" : { |
6 | 6 | "type": "hg", |
@@ -30,7 +30,7 @@ | ||
30 | 30 | |
31 | 31 | |
32 | 32 | Object property validation |
33 | - Version 0.0.2 | |
33 | + Version 0.0.3 | |
34 | 34 | Copyright (c) 2016, 2017 Frostbane Ac |
35 | 35 | See LICENSE.md. |
36 | 36 | www.??.com |
@@ -38,7 +38,7 @@ | ||
38 | 38 | |
39 | 39 | */ |
40 | 40 | |
41 | - /* | |
41 | +/* | |
42 | 42 | * 2016.08.12 |
43 | 43 | * frostbane |
44 | 44 | * |
@@ -105,6 +105,7 @@ | ||
105 | 105 | //"alpha_dash_dot", |
106 | 106 | //"alpha_slash_dot", |
107 | 107 | //"alpha_dash", |
108 | + "integer", | |
108 | 109 | "numeric", |
109 | 110 | //"boolean", |
110 | 111 | //"is_numeric", |
@@ -248,10 +249,26 @@ | ||
248 | 249 | |
249 | 250 | }, |
250 | 251 | |
252 | + integer :function(a, b){ | |
253 | + var getMessage = MicroCheck.message.numeric; | |
254 | + | |
255 | + var isInteger = new RegExp("^[\-+]?[0-9]*$").test(a) && | |
256 | + (a + "").split(".").length <= 2; | |
257 | + | |
258 | + return b ? | |
259 | + isInteger ? | |
260 | + false : | |
261 | + getMessage.call(this, b) : | |
262 | + !isInteger ? | |
263 | + false : | |
264 | + getMessage.call(this, b); | |
265 | + | |
266 | + }, | |
267 | + | |
251 | 268 | numeric :function(a, b){ |
252 | 269 | var getMessage = MicroCheck.message.numeric; |
253 | 270 | |
254 | - var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) && | |
271 | + var isNumeric = new RegExp("^[\-+]?[0-9]*(\.[0-9]*)?$").test(a) && | |
255 | 272 | (a + "").split(".").length <= 2; |
256 | 273 | |
257 | 274 | return b ? |
@@ -36,6 +36,7 @@ | ||
36 | 36 | name :"axas", |
37 | 37 | code :"C00001X-001", |
38 | 38 | id :1, |
39 | + cost :"1.", | |
39 | 40 | info :{ |
40 | 41 | address :"西梅田", |
41 | 42 | members :[ |
@@ -81,6 +82,7 @@ | ||
81 | 82 | name :"kds", |
82 | 83 | code :"C00002C", |
83 | 84 | id :2, |
85 | + cost :"2.0", | |
84 | 86 | info :{ |
85 | 87 | address :"本町", |
86 | 88 | members :[ |
@@ -108,6 +110,7 @@ | ||
108 | 110 | name :"ksi", |
109 | 111 | code :"C00003F", |
110 | 112 | id :3, |
113 | + cost :"3", | |
111 | 114 | info :{ |
112 | 115 | address :"大国町", |
113 | 116 | members :[ |
@@ -375,14 +378,14 @@ | ||
375 | 378 | |
376 | 379 | var validationRules = [ |
377 | 380 | [ |
378 | - "id of first company", | |
381 | + "id of the first company", | |
379 | 382 | "companies[0].id", |
380 | 383 | { |
381 | 384 | matches :"1", |
382 | 385 | }, |
383 | 386 | ], |
384 | 387 | [ |
385 | - "id of 2nd company", | |
388 | + "id of the 2nd company", | |
386 | 389 | "companies[1].id", |
387 | 390 | { |
388 | 391 | matches :2, |
@@ -799,6 +802,29 @@ | ||
799 | 802 | |
800 | 803 | }, |
801 | 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); | |
825 | + | |
826 | + }, | |
827 | + | |
802 | 828 | test_numeric :function(){ |
803 | 829 | "use strict"; |
804 | 830 |
@@ -810,6 +836,13 @@ | ||
810 | 836 | numeric :true, |
811 | 837 | }, |
812 | 838 | ], |
839 | + [ | |
840 | + "company cost", | |
841 | + "companies[*].cost", | |
842 | + { | |
843 | + numeric: true, | |
844 | + }, | |
845 | + ], | |
813 | 846 | ]; |
814 | 847 | |
815 | 848 | var cm = new MicroCheck(validationRules) |