• R/O
  • SSH

micro-check: Commit

input validation library


Commit MetaInfo

Revisionb62f0076534aeb6bc385ee5a4756b07d1ceed5fe (tree)
Time2018-04-12 19:11:40
Authorfrostbane <frostbane@prog...>
Commiterfrostbane

Log Message

fix numeric check bug

numeric now allows numbers with decimals
add integer check

Change Summary

Incremental Difference

diff -r f32fe31823f1 -r b62f0076534a package.json
--- a/package.json Thu Apr 12 18:36:29 2018 +0900
+++ b/package.json Thu Apr 12 19:11:40 2018 +0900
@@ -1,6 +1,6 @@
11 {
22 "name" : "micro-check",
3- "version" : "v0.0.2",
3+ "version" : "v0.0.3",
44 "description" : "Object property validation",
55 "repository" : {
66 "type": "hg",
diff -r f32fe31823f1 -r b62f0076534a src/microcheck.js
--- a/src/microcheck.js Thu Apr 12 18:36:29 2018 +0900
+++ b/src/microcheck.js Thu Apr 12 19:11:40 2018 +0900
@@ -30,7 +30,7 @@
3030
3131
3232 Object property validation
33- Version 0.0.2
33+ Version 0.0.3
3434 Copyright (c) 2016, 2017 Frostbane Ac
3535 See LICENSE.md.
3636 www.??.com
@@ -38,7 +38,7 @@
3838
3939 */
4040
41- /*
41+/*
4242 * 2016.08.12
4343 * frostbane
4444 *
@@ -105,6 +105,7 @@
105105 //"alpha_dash_dot",
106106 //"alpha_slash_dot",
107107 //"alpha_dash",
108+ "integer",
108109 "numeric",
109110 //"boolean",
110111 //"is_numeric",
@@ -248,10 +249,26 @@
248249
249250 },
250251
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+
251268 numeric :function(a, b){
252269 var getMessage = MicroCheck.message.numeric;
253270
254- var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
271+ var isNumeric = new RegExp("^[\-+]?[0-9]*(\.[0-9]*)?$").test(a) &&
255272 (a + "").split(".").length <= 2;
256273
257274 return b ?
diff -r f32fe31823f1 -r b62f0076534a test/microcheck.test.js
--- a/test/microcheck.test.js Thu Apr 12 18:36:29 2018 +0900
+++ b/test/microcheck.test.js Thu Apr 12 19:11:40 2018 +0900
@@ -36,6 +36,7 @@
3636 name :"axas",
3737 code :"C00001X-001",
3838 id :1,
39+ cost :"1.",
3940 info :{
4041 address :"西梅田",
4142 members :[
@@ -81,6 +82,7 @@
8182 name :"kds",
8283 code :"C00002C",
8384 id :2,
85+ cost :"2.0",
8486 info :{
8587 address :"本町",
8688 members :[
@@ -108,6 +110,7 @@
108110 name :"ksi",
109111 code :"C00003F",
110112 id :3,
113+ cost :"3",
111114 info :{
112115 address :"大国町",
113116 members :[
@@ -375,14 +378,14 @@
375378
376379 var validationRules = [
377380 [
378- "id of first company",
381+ "id of the first company",
379382 "companies[0].id",
380383 {
381384 matches :"1",
382385 },
383386 ],
384387 [
385- "id of 2nd company",
388+ "id of the 2nd company",
386389 "companies[1].id",
387390 {
388391 matches :2,
@@ -799,6 +802,29 @@
799802
800803 },
801804
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+
802828 test_numeric :function(){
803829 "use strict";
804830
@@ -810,6 +836,13 @@
810836 numeric :true,
811837 },
812838 ],
839+ [
840+ "company cost",
841+ "companies[*].cost",
842+ {
843+ numeric: true,
844+ },
845+ ],
813846 ];
814847
815848 var cm = new MicroCheck(validationRules)
Show on old repository browser