koa-input
v1.0.5
Published
a middleware for koa to validate input (query, param and body)
Downloads
63
Maintainers
Readme
koa-input
A middleware for koa to validate the input (query, params, body and headers etc.)
Stop to write CIERR(Check-If-Error-Return-Repeatedly) style code, it can be done automatically!
Install
Usage
- Quick start
var app = require('koa')();
var input = require('koa-input');
app.use(input('query', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//same as:
//app.use(input.query('name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
app.use(function *() {
this.body = this.request.query.name
});
- Support query, params(koa-router), body(koa-bodyparser) and headers etc.
var app = require('koa')();
var input = require('koa-input');
app.use(input('query', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('params', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('body', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('headers', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
app.use(function *() {
this.body = this.request.query.name;
//or
this.body = this.request.params.name;
//or
this.body = this.params.name;//You don't need to change existed koa-router code
//or
this.body = this.request.body.name;
//or
this.body = this.request.headers.name;
});
- Support custom error handler
input.onError(function (name) {
var error = new Error(util.format('Invalid get %s', name));
error.status = 200;
error.code = 77;
return error;
});
- Support special error
//String as error.message
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, 'invalid query name'));
//Number as error.status
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, 400));
//Object will be extended by input.error()
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, {status: 400, message: 'invalid query name'}));
//The same as Object
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, new Error('invalid query name')));
- Support default value as an optional input
//set a not-`undefined` default value (function will be call and using it's return value)
app.use(input('query', 'type', /^(cat|dog)$/, 'dog'));
app.use(input('query', 'time', /^[0-9]{13}$/, Date.now));
app.use(input('query', 'type', /^(cat|dog)$/, null));//you can pass `null` if you want a `nil` default value
//or
app.use(input('query', 'type', /^(cat|dog)$/, function() {}));//just pass an empty function to get a `undefined` as default value
- Support Regex, Function, Object, Array, Basic-Type pattern to validate the input
//Function(you can use any other module like validator)
app.use(input('query', 'email', validator.isEmail));
//Object(it will get the value if match the key)
app.use(input('query', 'status', {'normal': 0, 'invalid': 1}));
//Array(it must be element of the array)
app.use(input('query', 'type', ['cat', 'dog']));
//String(it must be equal to)
app.use(input('query', 'type', 'cat'));
- Support multiple patterns
app.use(input('query', 'type', ['cat', 'dog']));
app.use(input('query', 'type', {cat: 1, dog: 2}));
app.use(input('query', 'type', function (value) {
return value === 'cat';
}));
- Support builder or object options
//if you want to ignore some middle-arguments:
app.use(input('query', 'name', undefined, undefined, 'invalid input'));
//OR
app.use(input.source('query').name('name').error('invalid input').build());
//OR
app.use(input({source: 'query', name: 'name', error: 'invalid input'}));
//OR
app.use(input('query', {name: 'name', error: 'invalid input'}));
- Dependence on no-input