koa-validator v0.6.4
koa-validator
a koa port of express-validator
Install
npm install koa-validatorUsage
var koa = require('koa')
, validator = require('koa-validator')
, bodyParser = require('koa-bodyparser')
, app = koa()
;
app
.use(bodyParser())
.use(validator({
onValidationError: function(errMsg){
console.log('Validation error:', errMsg);
}
}))
.use(functon *(next){
this.checkParams('testparam', 'Invalid number').isInt();
yield next;
})
.listen(3000)
;Options
onValidationError -
function(errMsg), default to null. TheerrMsgis the errMsg you defined when you check one variableYou can define the function like this
function(errMsg){ throw new Error('Validation error: ' + errMsg); }validationErrorFormatter -
function(paramName, errMsg, value), the default function is belowfunction(paramName, errMsg, value){ return { param: paramName , msg: errMsg , value: value }; }
Note
If you will use checkBody or assertBody or sanitizeBody, you should use one bodyparse middleware before validator.
Test
npm testAPI
- checkParams
- checkQuery
- checkBody
- checkHeader
- sanitizeParams
- sanitizeQuery
- sanitizeBody
- assertParams => checkParams
- assertQuery => checkQuery
- assertBody => checkBody
- assertHeader => checkHeader
- haveValidationError, return
trueif have any validationError - validationErrors, if have any validationError, return an array of error object you returned in
validationErrorFormatter
Check
You can use all check methods in validator.js checks.
In addition, you can use below methods extended to validator.js.
notEmptyemptyeq, use==eqeq, use===neq, use!=neqeq, use!==gtltgelenotContainsisTimelen, an alias to validator.js'isLengthin, an alias to validator.js'isInbyteLength, a function support set charset.isUrl, an alias to validator.js'isURLisIp, an alias to validator.js'isIPisLowerCase, an alias to validator.js'isLowercaseisUpperCase, an alias to validator.js'isUppercase
Sanitize
You can use all sanitize methods in validator.js.
In addition, you can use below methods extended to validator.js.
default, set default valuetoLowerCasetoUpperCaseencodeURIencodeURIComponentdecodeURIdecodeURIComponentreplacetoLow, an alias totoLowerCasetoUp, an alias totoUpperCase
Extend
If you want to extend validator.js, you can use extendCheck and extendSanitize like below examples.
When you extend validator.js, your check function name best match is*, and your sanitize function name best match to*.
This is not force, but recommended.
If you extend one function that validator.js have already had, the function will be ignored, and an error will be thrown.
var koa = require('koa')
, bodyParser = require('koa-bodyparser')
, validator = require('koa-validator')
;
validator.extendCheck('isFinite', function(str){
return isFinite(str);
});
validator.extendCheck({
isFinite: function(str){
return isFinite(str);
}
, isFinite2: function(str){
return isFinite(str);
}
});
validator.extendSanitize('toZero', function(str){
return 0;
});
validator.extendSanitize({
toOne: function(str){
return 1;
}
, toTwo: function(str){
return 2;
}
});
koa()
.use(bodyParser())
.use(validator())
.use(function *(next){
this.checkParams('test', 'Invalid test value').isFinite();
yield next;
})
.listen(3000)
;Thanks
Thanks to koa-validate, some extended methods and test suites are based on koa-validate.
LICENSE
MIT
