2.0.0 • Published 10 years ago
koa-permit v2.0.0
koa-permit
A simple param filtering middleware. Inspired by Rails' strong parameters with support for nested filtering.
Installation
$ npm install --save koa-permit koa-bodyparser
import permit from 'koa-permit' // new syntax
const permit = require('koa-permit') // old syntaxBasic Usage
/*
 * app.js
 */
import koa from 'koa'
import router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import permit from 'koa-permit'
const app = koa()
const r = router()
r.post('/users',
  bodyParser(),
  permit('email', 'password'),
  function * () {
    const user = new User(this.request.body) // filtered params
    yield user.save()
    this.status = 201
  }
)
app.use(r.routes())Nested filtering
/*
 * routes/users.js
 */
import router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import permit from 'koa-permit'
const userParams = [
  'email',
  'password',
  'profile.firstname',
  'profile.lastname',
  'profile.address.street',
  'profile.address.number'
]
export default const r = router()
r.post('/users',
  bodyParser(),
  permit(userParams),
  function * () {
    const user = new User(this.request.body) // filtered params
    yield user.save()
    this.status = 201
  }
)
/*
 * app.js
 */
import koa from 'koa'
import users from 'routes/users'
const app = koa()
app.use(users.routes())Tests
$ npm testIssues
Feel free to submit issues and enhancement requests.
Contributing
- Fork the repo on GitHub
 - Commit changes to a branch in your fork
 - Pull request "upstream" with your changes
 - Merge changes in to "upstream" repo