Skip to content

Rails-style strong parameters for javascript projects (e.g. Express, Koa)

License

Notifications You must be signed in to change notification settings

DaPulse/strong-params

Repository files navigation

strong-params

Build Status NPM version

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.

Installation

Install the npm package.

npm install strong-params --save

Attach the middleware.

Express
var express = require('express')
var params = require('strong-params')
app.use(params.expressMiddleware())
Koa
var koa = require('koa')
var params = require('strong-params')
var app = koa()
app.use(params.koaMiddleware())

Usage

Get strong parameters

Express
app.use(function (req, res, next) {
  var params = req.parameters
})
Koa
app.use(function *() {
  var params = this.params
})
Standalone
var strongify = require('strong-params').strongify
var params = strongify({ key: 'value' })

Methods

// 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' }

Contributing

Please follow Contributing

About

Rails-style strong parameters for javascript projects (e.g. Express, Koa)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%