koa-validator
a koa port of express-validator
Install
npm install koa-validator
Usage
var koa = validator = bodyParser = app = ; app ;
Options
-
onValidationError -
function(errMsg)
, default to null. TheerrMsg
is 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 test
API
- checkParams
- checkQuery
- checkBody
- checkHeader
- sanitizeParams
- sanitizeQuery
- sanitizeBody
- assertParams => checkParams
- assertQuery => checkQuery
- assertBody => checkBody
- assertHeader => checkHeader
- haveValidationError, return
true
if 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.
notEmpty
empty
eq
, use==
eqeq
, use===
neq
, use!=
neqeq
, use!==
gt
lt
ge
le
notContains
isTime
len
, an alias to validator.js'isLength
in
, an alias to validator.js'isIn
byteLength
, a function support set charset.isUrl
, an alias to validator.js'isURL
isIp
, an alias to validator.js'isIP
isLowerCase
, an alias to validator.js'isLowercase
isUpperCase
, 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 valuetoLowerCase
toUpperCase
encodeURI
encodeURIComponent
decodeURI
decodeURIComponent
replace
toLow
, an alias totoLowerCase
toUp
, 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 = bodyParser = validator = ; validator;validator; validator;validator; ;
Thanks
Thanks to koa-validate
, some extended methods and test suites are based on koa-validate.
LICENSE
MIT