Koa middleware for setting rules HTTP method parameters to endpoints. Make parameter validation as easy-as-pil
npm install pil
Set parameter options;
| Key | Type | Description |
|---|---|---|
| name | String | Name of your parameter, you must include this in your options. |
| required | Boolean | Required if true, optional if false - true by default. |
| regex | Object (regex) | A regular expression to match with if parameter is string. |
| type | String | Parameter data type; boolean - number - string - object - array. |
| error | Object | Set custom error messages in specific scenarios. |
Here is an example application using pil to set parameters we want, we can be sure we will receive parameters in request.params as pil handles everything else.
'use strict';letkoa=require('koa'),mount=require('koa-mount'),Router=require('koa-router'),bodyParser=require('koa-bodyparser'),pil=require('pil'),app=koa()app.use(bodyParser())lettest=newRouter()app.use(mount('/',test.middleware()))test.get('/get',pil.set([{name: "foo",required: true,type: "string",error: {missing: "You need to provide foo otherwise the world will break"}},{name: "email",regex: /@/g}]),function*(){letfoo=this.request.params.foo,email=this.request.params.emailthis.body="hello world"})app.listen(3001)Check examples in test/ folder in both app.js for pure koa application using pil or rapp.js for routed app example like this quickstart.
Must be running node with
v4.3.*or higherFor POST / PUT methods pil works with the official
koa-bodyparserbest or other body parsing libraries which set data tothis.request.body. This libraries will extend to support pure koa post requests.
Authors; Ulpian Morina
