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 = ;var input = ;app;//same as://app.use(input.query('name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));app;
- Support query, params(koa-router), body(koa-bodyparser) and headers etc.
var app = ;var input = ;app;//orapp;//orapp;//orapp;app;
- Support custom error handler
input;
- Support special error
//String as error.messageapp;//Number as error.statusapp;//Object will be extended by input.error()app;//The same as Objectapp;
- Support default value as an optional input
//set a not-`undefined` default value (function will be call and using it's return value)app;app; app;//you can pass `null` if you want a `nil` default value//orapp;//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;//Object(it will get the value if match the key)app;//Array(it must be element of the array)app;//String(it must be equal to)app;
- Support multiple patterns
app;app;app;
- Support builder or object options
//if you want to ignore some middle-arguments:app;//ORapp;//ORapp;//ORapp;
- Dependence on no-input