JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3099
  • Score
    100M100P100Q122914F
  • License MIT

a koa params validate middleware.

Package Exports

  • koa-validate

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-validate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

koa-validate

Build Status Coverage Status NPM version dependencies

validate koa request params and format request params

##installation

$ npm install koa-validate

basic usage:

'use strict';
var koa = require('koa');
var app = koa();

app.use(require('koa-body')());
app.use(require('koa-validate')());
app.use(require('koa-router')(app));
app.post('/signup', function * () {
    //optional() means this param may be in the params.
    this.checkBody('name').optional().len(2, 20,"are you kidding me?");
    this.checkBody('email').isEmail("your enter a bad email.");
    this.checkBody('password').notEmpty().len(3, 20);
    //empty() mean this param can be a empty string.
    this.checkBody('nick').optional().empty().len(3, 20);
    this.checkBody('age').toInt();
    if (this.errors) {
        this.body = this.errors;
        return;
    }
    this.body = this.body;
});
app.get('/users', function * () {
    this.checkQuery('department').empty().in(["sale","finance"], "not support this department!").len(3, 20);	
    this.checkQuery('name').empty().len(2,20,"bad name.").trim().toLow();
    this.checkQuery('age').empty().gt(10,"too young!").lt(30,"to old!").toInt();
    if (this.errors) {
        this.body = this.errors;
        return;
    }
    this.body = this.query;
});
app.get('/user/:id', function * () {
    this.checkParams('id').toInt(0);
    if (this.errors) {
        this.body = this.errors;
        return;
    }
    this.body = this.params;
});

app.listen(3000);

how to extends validate