Skip to content

hiddentao/koa-validator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-validator

Build Status NPM version Dependency Status

NPM

a koa port of express-validator

Install

npm install koa-validator

Usage

var koa = require('koa')
    , validator = require('koa-validator')()
    , bodyParser = require('koa-bodyparser')
    , app = koa()
    ;

app
    .use(bodyParser())
    .use(function*(next) {
        this.onErrorCallback = function(errMsg) {
            throw new Error('Validation error: ' + errMsg);
        };
        
        yield validator.call(this, next);
    })
    .use(functon *(next){
        this.checkParams('testparam', 'Invalid number').isInt();
        yield next;
    })
    .listen(3000)

Note

If you will use checkBody or assertBody, 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
  • onValidationError
  • validationErrors

Check

You can use all check methods in validator.js.

Sanitize

You can use all sanitize methods in validator.js.

LICENSE

MIT

About

a koa port of express-validator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%