Skip to content

Repository files navigation

pil

Koa middleware for setting rules HTTP method parameters to endpoints. Make parameter validation as easy-as-pil

npm install pil

CircleCI



pil.set({opts})

Set parameter options;

KeyTypeDescription
nameStringName of your parameter, you must include this in your options.
requiredBooleanRequired if true, optional if false - true by default.
regexObject (regex)A regular expression to match with if parameter is string.
typeStringParameter data type; boolean - number - string - object - array.
errorObjectSet custom error messages in specific scenarios.

Quickstart

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 higher

  • For POST / PUT methods pil works with the official koa-bodyparser best or other body parsing libraries which set data to this.request.body. This libraries will extend to support pure koa post requests.

Authors; Ulpian Morina

About

Koa middleware for setting rules to HTTP method parameters to endpoints.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages