Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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"
after_script: "cat ./coverage/lcov.info | coveralls"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 11 additions & 9 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ 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;
};

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.length<min) {
Expand Down Expand Up @@ -247,8 +247,9 @@ Validator.prototype.isIp = function(tip) {
}
return this;
};
Validator.prototype.isAlpha = function(tip) {
if (this.goOn && !v.isAlpha(this.value)) {
Validator.prototype.isAlpha = function(locale, tip) {
locale = locale || 'en-US';
if (this.goOn && !v.isAlpha(this.value, locale)) {
this.addError(tip || this.key + " is not an alpha string.");
}
return this;
Expand All @@ -260,8 +261,9 @@ Validator.prototype.isNumeric = function(tip) {
return this;
};

Validator.prototype.isAlphanumeric = function(tip) {
if (this.goOn && !v.isAlphanumeric(this.value)) {
Validator.prototype.isAlphanumeric = function(locale, tip) {
locale = locale || 'en-US';
if (this.goOn && !v.isAlphanumeric(this.value, locale)) {
this.addError(tip || this.key + " is not an aphanumeric string.");
}
return this;
Expand Down Expand Up @@ -315,7 +317,7 @@ Validator.prototype.isByteLength = function(min, max,charset,tip) {
this.notEmpty(tip);
if (this.goOn) {
var bl = Buffer.byteLength(this.value , charset);
tip = !v.isInt(max) ? max : tip;
tip = (typeof max !== "number" && !v.isInt(max)) ? max : tip;
if (bl<min || bl>max) {
this.addError(tip || this.key + "'s byte lenth great than " + min +" and less than " + max + "." );
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
34 changes: 20 additions & 14 deletions test/test_validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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 ;
Expand All @@ -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",
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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*(){
Expand Down Expand Up @@ -425,5 +433,3 @@ describe('koa-validate' , function(){
.expect('ok' , done);
});
});