diff --git a/.travis.yml b/.travis.yml index 2db991f..447916c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: - - "0.11" + - "0.12" script: "npm run-script test-travis" -after_script: "cat ./coverage/lcov.info | coveralls" \ No newline at end of file +after_script: "cat ./coverage/lcov.info | coveralls" diff --git a/README.md b/README.md index 52f9f04..cf4e0e4 100644 --- a/README.md +++ b/README.md @@ -106,9 +106,9 @@ when use `app.use(require('koa-validate')())` ,the request context will bind the - **isEmail([tip])** - check if the param is an email. - **isUrl([tip])** - check if the param is an URL. - **isIp([tip])** - check if the param is an IP (version 4 or 6). -- **isAlpha([tip])** - check if the param contains only letters (a-zA-Z). +- **isAlpha([locale], [tip])** - check if the param contains only letters (a-zA-Z). - **isNumeric([tip])** - check if the param contains only numbers. -- **isAlphanumeric([tip])** - check if the param contains only letters and numbers. +- **isAlphanumeric([locale], [tip])** - check if the param contains only letters and numbers. - **isBase64([tip])** - check if a param is base64 encoded. - **isHexadecimal([tip])** - check if the param is a hexadecimal number. - **isHexColor([tip])** - check if the param is a hexadecimal color. diff --git a/lib/validate.js b/lib/validate.js index 95b4b2a..35624c4 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -139,13 +139,13 @@ Validator.prototype.ensure = function(assertion, tip, shouldBail) { }; Validator.prototype.isInt = function(tip) { - if (this.goOn&& !v.isInt(this.value)) { + if (this.goOn && typeof this.value !== 'number' && !v.isInt(this.value)) { this.addError(tip || this.key + " is not integer."); } return this; }; Validator.prototype.isFloat = function(tip) { - if (this.goOn && !v.isFloat(this.value)) { + if (this.goOn && typeof this.value !== 'number' && !v.isFloat(this.value)) { this.addError(tip || this.key + " is not float."); } return this; @@ -153,8 +153,8 @@ Validator.prototype.isFloat = function(tip) { Validator.prototype.isLength = function(min, max, tip) { min = min || 0; - tip = !v.isInt(max) ? max : tip; - max = v.isInt(max) ? max :-1; + tip = (typeof max !== "number" && !v.isInt(max)) ? max : tip; + max = (typeof max === "number" || v.isInt(max)) ? max : -1; this.exist(tip); if (this.goOn) { if(this.value.lengthmax) { this.addError(tip || this.key + "'s byte lenth great than " + min +" and less than " + max + "." ); } diff --git a/package.json b/package.json index fb0a2b1..66ec79d 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "main": "lib/validate.js", "scripts": { "test": "mocha -R spec --require should --harmony test/ --bail", - "test-cov": "node --harmony node_modules/istanbul-harmony/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -u exports --require should test/ --bail", - "test-travis": "node --harmony node_modules/istanbul-harmony/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -u exports --require should test/ --bail" + "test-cov": "node --harmony node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -u exports --require should test/ --bail", + "test-travis": "node --harmony node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -u exports --require should test/ --bail" }, "repository": { "type": "git", @@ -24,18 +24,18 @@ "license": "MIT", "dependencies": { "debug": "^2.0.0", - "validator": "^3.22.1" + "validator": "^5.0.0" }, "devDependencies": { "koa": "*", - "koa-router": "*", + "koa-router": "^4.3.2", "koa-body": "*", "should": "*", "mocha": "*", "supertest": "^0.13.0", "coveralls": "*", "mocha-lcov-reporter": "*", - "istanbul-harmony": "*" + "istanbul": "*" }, "engines": { "node": ">= 0.11.9" diff --git a/test/test_validate.js b/test/test_validate.js index fcb0649..7336f18 100644 --- a/test/test_validate.js +++ b/test/test_validate.js @@ -14,7 +14,7 @@ function create_app(){ } describe('koa-validate' , function(){ - it("these validates should be to ok" , function(done){ + it("these validations should pass" , function(done){ var app = create_app(); app.post('/validate',function*(){ this.checkBody('optional').optional().len(3,20); @@ -41,8 +41,10 @@ describe('koa-validate' , function(){ this.checkBody('url').isUrl(); this.checkBody('ip').isIp(); this.checkBody('alpha').isAlpha(); + this.checkBody('alphaDe').isAlpha('de-DE'); this.checkBody('numeric').isNumeric(); this.checkBody('an').isAlphanumeric(); + this.checkBody('anDe').isAlphanumeric('de-DE'); this.checkBody('base64').isBase64(); this.checkBody('hex').isHexadecimal(); this.checkBody('color1').isHexColor(); @@ -58,8 +60,8 @@ describe('koa-validate' , function(){ this.checkBody('uuid').isUUID(); this.checkBody('date').isDate(); this.checkBody('time').isTime(); - this.checkBody('after').isAfter(new Date("2014-08-06")); - this.checkBody('before').isBefore(new Date("2014-08-08")); + this.checkBody('after').isAfter("2014-08-06"); + this.checkBody('before').isBefore("2014-08-08"); this.checkBody('in').isIn(); this.checkBody('credit').isCreditCard(); this.checkBody('isbn').isISBN(); @@ -102,8 +104,10 @@ describe('koa-validate' , function(){ url:"http://www.google.com", ip:'192.168.1.1', alpha:"abxyABXZ", + alphaDe:"tschüß", numeric:"3243134", an:"a1b2c3", + anDe:"a1b2c3ü4ß6", base64:"aGVsbG8=", hex:"0a1b2c3ef", color1:"#ffffff", @@ -134,7 +138,7 @@ describe('koa-validate' , function(){ .expect('ok' ,done); }); - it("these validates fail tests should be to ok" , function(done){ + it("these validations should fail" , function(done){ var app = create_app(); app.post('/validate',function*(){ this.checkBody('name').notEmpty().len(3,20); @@ -156,8 +160,10 @@ describe('koa-validate' , function(){ this.checkBody('url').isUrl(); this.checkBody('ip').isIp(); this.checkBody('alpha').isAlpha(); + this.checkBody('alphaDe').isAlpha('de-DE'); this.checkBody('numeric').isNumeric(); this.checkBody('an').isAlphanumeric(); + this.checkBody('anDe').isAlphanumeric('de-DE'); this.checkBody('base64').isBase64(); this.checkBody('hex').isHexadecimal(); this.checkBody('color1').isHexColor(); @@ -174,8 +180,8 @@ describe('koa-validate' , function(){ this.checkBody('uuid').isUUID(); this.checkBody('time').isTime(); this.checkBody('date').isDate(); - this.checkBody('after').isAfter(new Date("2014-08-06")); - this.checkBody('before').isBefore(new Date("2014-08-02")); + this.checkBody('after').isAfter("2014-08-06"); + this.checkBody('before').isBefore("2014-08-02"); this.checkBody('in').isIn([1,2]); this.checkBody('credit').isCreditCard(); this.checkBody('isbn').isISBN(); @@ -187,7 +193,7 @@ describe('koa-validate' , function(){ this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); console.log(this.errors) - if(this.errors.length === 49){ + if(this.errors.length === 51){ this.body = this.errors; this.body = 'ok'; return ; @@ -210,13 +216,15 @@ describe('koa-validate' , function(){ eq:"neq", neq:'eq', number4:'4', - contains:"hello" , + contains:"hello" , notContains:"h f", url:"google", ip:'192.168.', alpha:"321", + alphaDe:"řeč", numeric:"fada", an:"__a", + anDe:"__a", base64:"fdsaf", hex:"hgsr", color1:"#fffff", @@ -246,8 +254,8 @@ describe('koa-validate' , function(){ .expect(200) .expect('ok' ,done); }); - - it('there validate query should be to okay' , function(done){ + + it('these query validations should pass' , function(done){ var app = create_app(); app.get('/query',function*(){ this.checkQuery('name').notEmpty(); @@ -266,7 +274,7 @@ describe('koa-validate' , function(){ }).expect(200) .expect('ok' , done); }); - it('there validate params should be to okay' , function(done){ + it('these params validations should pass' , function(done){ var app = create_app(); app.get('/:id',function*(){ this.checkParams('id').isInt(); @@ -281,7 +289,7 @@ describe('koa-validate' , function(){ .expect(200) .expect('ok' , done); }); - it('there sanitizers should be to okay' , function(done){ + it('these sanitizers should pass' , function(done){ var app = create_app(); var url ="http://www.google.com/" app.post('/sanitizers',function*(){ @@ -425,5 +433,3 @@ describe('koa-validate' , function(){ .expect('ok' , done); }); }); - -