koa-validator
Advanced tools
Comparing version 0.4.0 to 0.5.0
var validator = require('validator') | ||
, is = require('jistype') | ||
, extend = require('./extend') | ||
, sanitizers = [ | ||
'trim' | ||
, 'ltrim' | ||
, 'rtrim' | ||
, 'escape' | ||
, 'stripLow' | ||
, 'whitelist' | ||
, 'blacklist' | ||
, 'normalizeEmail' | ||
] | ||
, checkers = [ | ||
'equals' | ||
, 'contains' | ||
, 'matches' | ||
] | ||
; | ||
var koaValidator = function(options){ | ||
extendCheck(extend.check); | ||
extendSanitize(extend.sanitize); | ||
function koaValidator(options){ | ||
options = options || {}; | ||
@@ -15,12 +37,2 @@ | ||
var sanitizers = [ | ||
'trim' | ||
, 'ltrim' | ||
, 'rtrim' | ||
, 'escape' | ||
, 'stripLow' | ||
, 'whitelist' | ||
, 'blacklist' | ||
]; | ||
function getParams(ctx, name){ | ||
@@ -50,2 +62,3 @@ return ctx.params && ctx.params[name]; | ||
function updateParams(ctx, name, value){ | ||
return ctx.params[name] = value; | ||
// route params like /user/:id | ||
@@ -60,2 +73,3 @@ if (ctx.params && ctx.params.hasOwnProperty(name) && | ||
function updateQuery(ctx, name, value){ | ||
return ctx.query[name] = value; | ||
// query string params | ||
@@ -69,2 +83,3 @@ if (undefined !== ctx.query[name]) { | ||
function updateBody(ctx, name, value){ | ||
return ctx.request.body[name] = value; | ||
// request body params via connect.bodyParser | ||
@@ -153,3 +168,3 @@ if (ctx.request.body && undefined !== ctx.request.body[name]) { | ||
Object.keys(validator).forEach(function(methodName) { | ||
if (!methodName.match(/^to/) && sanitizers.indexOf(methodName) === -1) { | ||
if (methodName.match(/^is/) || checkers.indexOf(methodName) !== -1) { | ||
methods[methodName] = function() { | ||
@@ -168,10 +183,2 @@ var args = [value].concat(Array.prototype.slice.call(arguments)); | ||
methods['notEmpty'] = function() { | ||
return methods.isLength(1); | ||
}; | ||
methods['len'] = function() { | ||
return methods.isLength.apply(methods.isLength, Array.prototype.slice.call(arguments)); | ||
}; | ||
return methods; | ||
@@ -218,5 +225,60 @@ }; | ||
}; | ||
}; | ||
} | ||
function extendCheck(name, fn){ | ||
var objs = {}; | ||
if(is.isString(name) && is.isFunction(fn)){ | ||
obj[name] = fn; | ||
}else if(is.isObject(name)){ | ||
objs = name; | ||
} | ||
Object.keys(objs).forEach(function(name){ | ||
if(validator.hasOwnProperty(name)){ | ||
throw new Error('validator already have method ' + name); | ||
return; | ||
} | ||
if(checkers.indexOf(name) !== -1){ | ||
throw new Error('you have already defined method ' + name); | ||
return; | ||
} | ||
if(!name.match(/^is/)){ | ||
checkers.push(name); | ||
} | ||
validator.extend(name, objs[name]); | ||
}); | ||
} | ||
function extendSanitize(name, fn){ | ||
var objs = {}; | ||
if(is.isString(name) && is.isFunction(fn)){ | ||
obj[name] = fn; | ||
}else if(is.isObject(name)){ | ||
objs = name; | ||
} | ||
Object.keys(objs).forEach(function(name){ | ||
if(validator.hasOwnProperty(name)){ | ||
throw new Error('validator already have method ' + name); | ||
return; | ||
} | ||
if(sanitizers.indexOf(name) !== -1){ | ||
throw new Error('you have already defined method ' + name); | ||
return; | ||
} | ||
if(!name.match(/^to/)){ | ||
sanitizers.push(name); | ||
} | ||
validator.extend(name, objs[name]); | ||
}); | ||
} | ||
module.exports = koaValidator; | ||
module.exports.validator = validator; | ||
module.exports.extendSanitize = extendSanitize; | ||
module.exports.extendCheck = extendCheck; |
{ | ||
"name": "koa-validator", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "a koa port of express-validator", | ||
@@ -18,10 +18,11 @@ "main": "index.js", | ||
"chai": "^1.9.1", | ||
"koa": "^0.8.0", | ||
"koa": "^0.10.0", | ||
"koa-bodyparser": "^1.0.1", | ||
"koa-router": "^3.1.4", | ||
"mocha": "^1.20.1", | ||
"koa-router": "^3.2.3", | ||
"mocha": "^1.21.4", | ||
"supertest": "^0.13.0" | ||
}, | ||
"dependencies": { | ||
"validator": "^3.16.0" | ||
"jistype": "^0.1.0", | ||
"validator": "^3.17.0" | ||
}, | ||
@@ -28,0 +29,0 @@ "directories": { |
475
test/test.js
@@ -18,168 +18,369 @@ var koa = require('koa') | ||
describe('validator', function(){ | ||
it('should parse params', function(done){ | ||
it("these validates should be to ok" , function(done){ | ||
var app = createApp(); | ||
app.get('/:testparam', function *(next){ | ||
this.checkParams('testparam', 'Parameter is not an integer').isInt(); | ||
this.sanitizeParams('testparam').toInt(); | ||
var errors = this.validationErrors(); | ||
if(errors){ | ||
this.body = errors; | ||
}else{ | ||
this.body = { | ||
testparam: this.params.testparam | ||
}; | ||
app.post('/validate',function*(){ | ||
this.checkBody('name').notEmpty().len(3,20); | ||
this.checkBody('empty').empty(); | ||
this.checkBody('match').matches(/^abc$/i); | ||
this.checkBody('integer').isInt(); | ||
this.checkBody('float_').isFloat(); | ||
this.checkBody('in').in([1,2]); | ||
this.checkBody('eq').eq("eq"); | ||
this.checkBody('neq').neq("eq"); | ||
this.checkBody('number4').gt(3); | ||
this.checkBody('number4').lt(5); | ||
this.checkBody('number4').ge(4); | ||
this.checkBody('number4').le(4); | ||
this.checkBody('number4').ge(3); | ||
this.checkBody('number4').le(5); | ||
this.checkBody('contains').contains("tain"); | ||
this.checkBody('notContains').notContains(" "); | ||
this.checkBody('email').isEmail(); | ||
this.checkBody('url').isUrl(); | ||
this.checkBody('ip').isIp(); | ||
this.checkBody('alpha').isAlpha(); | ||
this.checkBody('numeric').isNumeric(); | ||
this.checkBody('an').isAlphanumeric(); | ||
this.checkBody('base64').isBase64(); | ||
this.checkBody('hex').isHexadecimal(); | ||
this.checkBody('color1').isHexColor(); | ||
this.checkBody('color2').isHexColor(); | ||
this.checkBody('color3').isHexColor(); | ||
this.checkBody('color4').isHexColor(); | ||
this.checkBody('low').isLowercase(); | ||
this.checkBody('up').isUppercase(); | ||
this.checkBody('div').isDivisibleBy(3); | ||
this.checkBody('n').isNull(); | ||
this.checkBody('len').isLength(1,4); | ||
this.checkBody('byteLength').isByteLength(4,6); | ||
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('in').isIn([1, 2]); | ||
this.checkBody('credit').isCreditCard(); | ||
this.checkBody('isbn').isISBN(); | ||
this.checkBody('json').isJSON(); | ||
this.checkBody('mb').isMultibyte(); | ||
this.checkBody('ascii').isAscii(); | ||
this.checkBody('fw').isFullWidth(); | ||
this.checkBody('hw').isHalfWidth(); | ||
this.checkBody('vw').isVariableWidth(); | ||
this.checkBody('sp').isSurrogatePair(); | ||
if(this._validationErrors){ | ||
this.body = this._validationErrors; | ||
return; | ||
} | ||
this.body= 'ok'; | ||
}); | ||
var req = request(app.listen()); | ||
request(app.listen()) | ||
.get('/123') | ||
.expect(function(res){ | ||
res.body.should.deep.equal({ | ||
testparam: 123 | ||
}); | ||
}) | ||
.end(done) | ||
; | ||
req.post('/validate') | ||
.send({ | ||
name:"jim", | ||
empty:"", | ||
email:"jim@gmail.com", | ||
len:"len", | ||
match:"abc", | ||
integer:12, | ||
float_:1.23, | ||
in:1, | ||
eq:"eq", | ||
neq:'neq', | ||
number4:'4', | ||
contains:"contains", | ||
notContains:"notContains", | ||
url:"http://www.google.com", | ||
ip:'192.168.1.1', | ||
alpha:"abxyABXZ", | ||
numeric:"3243134", | ||
an:"a1b2c3", | ||
base64:"aGVsbG8=", | ||
hex:"0a1b2c3ef", | ||
color1:"#ffffff", | ||
color2:"ffffff", | ||
color3:"#fff", | ||
color4:"fff", | ||
low:"hello", | ||
up:"HELLO", | ||
div:"21", | ||
n:"", | ||
byteLength:"你好", | ||
uuid:"c8162b90-fdda-4803-843b-ed5851480c86", | ||
time:"13:12:00", | ||
date:"2014-08-07", | ||
after:"2014-08-07", | ||
before:"2014-08-07", | ||
credit:"4063651340421805", | ||
isbn:"9787513300711", | ||
json:'{"a":1}', | ||
mb:"多字节", | ||
ascii:"fff", | ||
fw:"宽字节", | ||
hw:"a字节", | ||
vw:"v多字节", | ||
sp:'ABC千𥧄1-2-3' | ||
}) | ||
.expect(200) | ||
.expect('ok' ,done); | ||
}); | ||
it('should parse query', function(done){ | ||
it("these validates fail tests should be to ok" , function(done){ | ||
var app = createApp(); | ||
app.get('/test', function *(next){ | ||
this.checkQuery('testparam', 'Parameter is not an integer').isInt(); | ||
this.sanitizeQuery('testparam').toInt(); | ||
var errors = this.validationErrors(); | ||
if(errors){ | ||
this.body = errors; | ||
}else{ | ||
this.body = { | ||
testparam: this.query.testparam | ||
}; | ||
app.post('/validate',function*(){ | ||
this.checkBody('name').notEmpty().len(3,20); | ||
this.checkBody('notEmpty').notEmpty(); | ||
this.checkBody('notEmpty').len(2,3); | ||
this.checkBody('match').matches(/^abc$/i); | ||
this.checkBody('integer').isInt(); | ||
this.checkBody('float_').isFloat(); | ||
this.checkBody('in').in([1,2]); | ||
this.checkBody('eq').eq("eq"); | ||
this.checkBody('neq').neq("eq"); | ||
this.checkBody('number4').gt(5); | ||
this.checkBody('number4').lt(3); | ||
this.checkBody('number4').ge(5); | ||
this.checkBody('number4').le(3); | ||
this.checkBody('contains').contains("tain"); | ||
this.checkBody('notContains').notContains(" "); | ||
this.checkBody('email').isEmail(); | ||
this.checkBody('url').isUrl(); | ||
this.checkBody('ip').isIp(); | ||
this.checkBody('alpha').isAlpha(); | ||
this.checkBody('numeric').isNumeric(); | ||
this.checkBody('an').isAlphanumeric(); | ||
this.checkBody('base64').isBase64(); | ||
this.checkBody('hex').isHexadecimal(); | ||
this.checkBody('color1').isHexColor(); | ||
this.checkBody('color2').isHexColor(); | ||
this.checkBody('color3').isHexColor(); | ||
this.checkBody('color4').isHexColor(); | ||
this.checkBody('low').isLowercase(); | ||
this.checkBody('up').isUppercase(); | ||
this.checkBody('div').isDivisibleBy(3); | ||
this.checkBody('n').isNull(); | ||
this.checkBody('len').isLength(3,4); | ||
this.checkBody('byteLength').isByteLength(4,6); | ||
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('in').isIn([1,2]); | ||
this.checkBody('credit').isCreditCard(); | ||
this.checkBody('isbn').isISBN(); | ||
this.checkBody('json').isJSON(); | ||
this.checkBody('mb').isMultibyte(); | ||
this.checkBody('ascii').isAscii(); | ||
this.checkBody('fw').isFullWidth(); | ||
this.checkBody('hw').isHalfWidth(); | ||
this.checkBody('vw').isVariableWidth(); | ||
this.checkBody('sp').isSurrogatePair(); | ||
if(this._validationErrors.length === 48){ | ||
this.body = this._validationErrors; | ||
this.body = 'ok'; | ||
return ; | ||
} | ||
this.body= 'only '+this._validationErrors.length+' errors'; | ||
}); | ||
var req = request(app.listen()); | ||
async.parallel([ | ||
function(done){ | ||
request(app.listen()) | ||
.get('/test?testparam=123') | ||
.expect(function(res){ | ||
res.body.should.deep.equal({ | ||
testparam: 123 | ||
}); | ||
}) | ||
.end(done) | ||
; | ||
} | ||
, function(done){ | ||
request(app.listen()) | ||
.get('/test?testparam=gettest') | ||
.expect(function(res){ | ||
res.body[0].msg.should.equal('Parameter is not an integer') | ||
}) | ||
.end(done) | ||
; | ||
} | ||
], done); | ||
req.post('/validate') | ||
.send({ | ||
name:"j", | ||
empty:"fd", | ||
email:"jim@@gmail.com", | ||
len:"l", | ||
match:"xyz", | ||
integer:"12a", | ||
float_:'a1.23', | ||
in:'fd', | ||
eq:"neq", | ||
neq:'eq', | ||
number4:'4', | ||
contains:"hello" , | ||
notContains:"h f", | ||
url:"google", | ||
ip:'192.168.', | ||
alpha:"321", | ||
numeric:"fada", | ||
an:"__a", | ||
base64:"fdsaf", | ||
hex:"hgsr", | ||
color1:"#fffff", | ||
color2:"fffff", | ||
color3:"#ff", | ||
color4:"ff", | ||
low:"Hre", | ||
up:"re", | ||
div:"22", | ||
n:"f", | ||
byteLength:"你", | ||
uuid:"c8162b90-fdda-4803-843bed5851480c86", | ||
date:"2014-0807", | ||
time:"24:00:00", | ||
after:"2014-08-05", | ||
before:"2014-08-02", | ||
credit:"4063651340421805332", | ||
isbn:"978751330071154", | ||
json:'{"a:1}', | ||
mb:"fd", | ||
ascii:"你好", | ||
fw:"43", | ||
hw:"你好", | ||
vw:"aa", | ||
sp:'fdfd' | ||
}) | ||
.expect(200) | ||
.expect('ok' ,done); | ||
}); | ||
it('should parse body', function(done){ | ||
it('there validate query should be to okay' , function(done){ | ||
var app = createApp(); | ||
app.post('/test', function *(next){ | ||
this.checkBody('testparam', 'Parameter is not an integer').isInt(); | ||
this.sanitizeBody('testparam').toInt(); | ||
var errors = this.validationErrors(); | ||
if(errors){ | ||
this.body = errors; | ||
}else{ | ||
this.body = { | ||
testparam: this.request.body.testparam | ||
}; | ||
app.get('/query',function*(){ | ||
this.checkQuery('name').notEmpty(); | ||
this.checkQuery('password').len(3,20); | ||
if(this.errors){ | ||
this.body = this.errors; | ||
return; | ||
} | ||
this.body = 'ok'; | ||
}); | ||
async.parallel([ | ||
function(done){ | ||
request(app.listen()) | ||
.post('/test') | ||
.send({ | ||
testparam: '123' | ||
}) | ||
.expect(function(res){ | ||
res.body.should.deep.equal({ | ||
testparam: 123 | ||
}); | ||
}) | ||
.end(done) | ||
; | ||
} | ||
, function(done){ | ||
request(app.listen()) | ||
.post('/test') | ||
.send({ | ||
testparam: 'gettest' | ||
}) | ||
.expect(function(res){ | ||
res.body[0].msg.should.equal('Parameter is not an integer'); | ||
}) | ||
.end(done) | ||
; | ||
} | ||
], done); | ||
request(app.listen()) | ||
.get('/query') | ||
.query({ | ||
name:'jim', | ||
password:'yeap' | ||
}).expect(200) | ||
.expect('ok' , done); | ||
}); | ||
it('should throw error when set onValidationError cbk', function(done){ | ||
it('there validate params should be to okay' , function(done){ | ||
var app = createApp(); | ||
app.get('/test', function *(next){ | ||
this.onValidationError(function(msg){ | ||
this.throw(402, msg); | ||
}); | ||
this.checkQuery('testparam', 'Parameter is not an integer').isInt(); | ||
app.get('/:id',function*(){ | ||
this.checkParams('id').isInt(); | ||
if(this._validationErrors){ | ||
this.body = this._validationErrors; | ||
return; | ||
} | ||
this.body = 'ok'; | ||
}); | ||
request(app.listen()) | ||
.get('/test?testparam=gettest') | ||
.expect(402, 'Parameter is not an integer') | ||
.end(done) | ||
; | ||
.get('/123') | ||
.expect(200) | ||
.expect('ok' , done); | ||
}); | ||
it('should return mapped errors', function(done){ | ||
it('there sanitizers should be to okay' , function(done){ | ||
var app = createApp(); | ||
var url ="http://www.google.com/" | ||
app.post('/sanitizers',function*(){ | ||
this.sanitizeBody('default').default('default'); | ||
this.sanitizeBody('int_').toInt(); | ||
this.sanitizeBody('float_').toFloat(); | ||
this.sanitizeBody('bool').toBoolean(); | ||
this.sanitizeBody('date').toDate(); | ||
this.sanitizeBody('trim').trim(); | ||
this.sanitizeBody('ltrim').ltrim(); | ||
this.sanitizeBody('rtrim').rtrim(); | ||
this.sanitizeBody('up').toUp(); | ||
this.sanitizeBody('low').toLow(); | ||
this.sanitizeBody('escape').escape(); | ||
this.sanitizeBody('stripLow').stripLow(); | ||
this.sanitizeBody('whitelist').whitelist('ll'); | ||
this.sanitizeBody('blacklist').blacklist('ll'); | ||
this.sanitizeBody('encodeURI').decodeURI(); | ||
this.sanitizeBody('decodeURI').encodeURI(); | ||
this.sanitizeBody('encodeURIComponent').decodeURIComponent(); | ||
this.sanitizeBody('decodeURIComponent').encodeURIComponent(); | ||
this.sanitizeBody('rep').replace(',' ,''); | ||
app.get('/test', function *(next){ | ||
this.checkQuery('testparam', 'Parameter is not an integer').isInt(); | ||
var body = this.request.body; | ||
var errors = this.validationErrors(true); | ||
if(errors){ | ||
this.body = errors; | ||
}else{ | ||
this.body = { | ||
testparam: this.query.testparam | ||
}; | ||
if('default' != body.default){ | ||
this.throw(500); | ||
} | ||
if(20 !== body.int_ ){ | ||
this.throw(500); | ||
} | ||
if(1.2 !== body.float_ ){ | ||
this.throw(500); | ||
} | ||
if(true!== body.bool ){ | ||
this.throw(500); | ||
} | ||
if(new Date('2014-01-01').getTime() !== body.date.getTime() ){ | ||
this.throw(500); | ||
} | ||
if('jim'!=body.trim){ | ||
this.throw(500); | ||
} | ||
if('jim '!=body.ltrim){ | ||
this.throw(500); | ||
} | ||
if(' jim'!=body.rtrim){ | ||
this.throw(500); | ||
} | ||
if('JIM'!=body.up){ | ||
this.throw(500); | ||
} | ||
if('jim'!=body.low){ | ||
this.throw(500); | ||
} | ||
if('<div>'!=body.escape){ | ||
this.throw(500); | ||
} | ||
if('abc'!=body.stripLow){ | ||
this.throw(500); | ||
} | ||
if('ll'!=body.whitelist){ | ||
this.throw(500); | ||
} | ||
if('heo'!=body.blacklist){ | ||
this.throw(500); | ||
} | ||
if(encodeURI(url)!=body.decodeURI){ | ||
this.throw(500); | ||
} | ||
if(decodeURI(url)!=body.encodeURI){ | ||
this.throw(500); | ||
} | ||
if(encodeURIComponent(url)!=body.decodeURIComponent){ | ||
this.throw(500); | ||
} | ||
if(decodeURIComponent(url)!=body.encodeURIComponent){ | ||
this.throw(500); | ||
} | ||
if('ab'!=body.rep){ | ||
this.throw(500); | ||
} | ||
this.body = 'ok'; | ||
}); | ||
request(app.listen()) | ||
.post('/sanitizers') | ||
.send({ | ||
int_:'20', | ||
float_:'1.2', | ||
bool:'1', | ||
date:'2014-01-01', | ||
trim:' jim ', | ||
ltrim:' jim ', | ||
rtrim:' jim ', | ||
up:'jim', | ||
low:'Jim', | ||
escape:'<div>', | ||
stripLow:'abc\r', | ||
whitelist:'hello', | ||
blacklist:'hello', | ||
encodeURI:encodeURI(url), | ||
decodeURI:url, | ||
encodeURIComponent:encodeURIComponent(url), | ||
decodeURIComponent:url, | ||
rep:'a,b' | ||
request(app.listen()) | ||
.get('/test?testparam=gettest') | ||
.expect(function(res){ | ||
res.body.should.deep.equal({ | ||
testparam: { | ||
param: 'testparam' | ||
, msg: 'Parameter is not an integer' | ||
, value: 'gettest' | ||
} | ||
}); | ||
}) | ||
.end(done) | ||
; | ||
}).expect(200) | ||
.expect('ok' , done); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25810
10
706
2
1
+ Addedjistype@^0.1.0
+ Addedcoffee-script@1.12.7(transitive)
+ Addedjistype@0.1.0(transitive)
Updatedvalidator@^3.17.0