Rails-style implementation of strong parameters (forked from koa-strong-params). It supports Express, Koa and also can be used as standalone. The middleware adds the parameters
object to the Express request (or this.params
for Koa context) which returns an object, built from query string
, request body
and route params
data. The returned object has some useful methods allows for data requiring
and filtering
.
Install the npm package.
npm install strong-params --save
var express = require('express')
var params = require('strong-params')
app.use(params.expressMiddleware())
var koa = require('koa')
var params = require('strong-params')
var app = koa()
app.use(params.koaMiddleware())
app.use(function (req, res, next) {
var params = req.parameters
})
app.use(function *() {
var params = this.params
})
var strongify = require('strong-params').strongify
var params = strongify({ key: 'value' })
// All available params
params.all()
// -> { id: '13', name: 'Bob', age: '13', email: '[email protected]', address: { country: 'US', street: '261 West' }}
// Only selected params (also has `only` as alias for `permit`)
params.permit('name', 'age')
// -> { name: 'Bob', age: '13' }
// All params except those provided
params.except('name', 'id', 'address')
// -> { id: '13', age: '13', email: '[email protected]' }
// All params of a sub-object
params.require('address').all()
// -> { country: 'US', street: '261 West' }
// Only selected params + some merged attributes
params.merge({ badge: 'coder' }).permit('name')
// -> { name: 'Bob', badge: 'coder' }
Please follow Contributing