0.1.1 • Published 8 years ago

pil v0.1.1

Weekly downloads
7
License
GPL-3.0
Repository
github
Last release
8 years ago

pil

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

npm install pil

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';

let koa = require('koa'),
    mount = require('koa-mount'),
    Router = require('koa-router'),
    bodyParser = require('koa-bodyparser'),
    pil = require('pil'),
    app = koa()

app.use(bodyParser())

let test = new Router()

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 * () {
    let foo = this.request.params.foo,
        email = this.request.params.email

    this.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 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