strong-params
Advanced tools
Comparing version 0.7.1 to 1.0.0
@@ -13,3 +13,2 @@ var _ = require('lodash') | ||
return function (req, res, next) { | ||
/** | ||
@@ -67,2 +66,1 @@ * Params data. | ||
} | ||
@@ -7,3 +7,3 @@ var _ = require('lodash') | ||
* | ||
* @return {generator} | ||
* @return {Function} | ||
* @api public | ||
@@ -13,4 +13,3 @@ */ | ||
module.exports = function () { | ||
return function *(next) { | ||
return function (ctx, next) { | ||
/** | ||
@@ -26,3 +25,3 @@ * Params data. | ||
Object.defineProperty(this, 'params', { | ||
Object.defineProperty(ctx, 'parameters', { | ||
@@ -57,6 +56,7 @@ /** | ||
* enable body params, which are usually received over `post` or `put` | ||
* method, use `koa-bodyparser` middleware. | ||
* method, use `koa-bodyparser` middleware. To enable route params, | ||
* use `koa-router` middleware. | ||
*/ | ||
this.params = _.merge({}, this.request.body, this.query) | ||
ctx.parameters = _.merge({}, ctx.request.body, ctx.query, ctx.params) | ||
@@ -67,4 +67,4 @@ /* | ||
yield next | ||
return next() | ||
} | ||
} |
@@ -169,3 +169,3 @@ /** | ||
_fetch: function (key) {return this._params[key] }, | ||
_fetch: function (key) { return this._params[key] }, | ||
@@ -202,4 +202,9 @@ /** | ||
for (var key in filters) { | ||
var param, isArrObj, filtersArray = filters[key] | ||
var param | ||
var isArrObj | ||
var filtersArray = filters[key] | ||
if (_.isArray(filtersArray) && (param = this._fetch(key))) { | ||
if (Parameters._isPrimitive(param)) { | ||
continue | ||
} | ||
if (_.isArray(param._params) || (isArrObj = Object.keys(param._params).every(function (i) { return !_.isNaN(Number(i)) }))) { | ||
@@ -206,0 +211,0 @@ if (isArrObj) { |
{ | ||
"name": "strong-params", | ||
"version": "0.7.1", | ||
"version": "1.0.0", | ||
"description": "Rails-style strong parameters for javascript projects. (e.g. Express, Koa)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && mocha --harmony --recursive ./spec" | ||
"test": "standard && mocha --recursive ./spec" | ||
}, | ||
"engines": { | ||
"node": ">= 0.11.9" | ||
"node": "8" | ||
}, | ||
@@ -43,2 +43,3 @@ "repository": { | ||
"contributors": [ | ||
"simonratner", | ||
"vellotis" | ||
@@ -52,7 +53,7 @@ ], | ||
"devDependencies": { | ||
"body-parser": "~1.10.2", | ||
"express": "~4.11.1", | ||
"koa": "^0.10.0", | ||
"koa-bodyparser": "^1.0.1", | ||
"koa-qs": "^1.0.0", | ||
"body-parser": "^1.17.2", | ||
"express": "^4.15.3", | ||
"koa": "^2.3.0", | ||
"koa-bodyparser": "^4.2.0", | ||
"koa-qs": "^2.0.0", | ||
"mocha": "^3.0.2", | ||
@@ -62,8 +63,8 @@ "request": "^2.40.0", | ||
"sinon": "^1.17.5", | ||
"standard": "^3.0.0" | ||
"standard": "^10.0.2" | ||
}, | ||
"dependencies": { | ||
"es6-object-assign": "^1.0.2", | ||
"lodash": "^3.0.0" | ||
"lodash": "^4.17.4" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # strong-params | ||
Rails-style implementation of strong parameters. It supports [Express](http://expressjs.com/), [Koa](https://github.com/koajs/koa) and also can be used as standalone. The middleware adds the `parameters` object to the [Express request](http://expressjs.com/4x/api.html#req) (or `this.params` for [Koa context](http://koajs.com/#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`. | ||
Rails-style implementation of strong parameters. It supports [Express](http://expressjs.com/), [Koa](https://github.com/koajs/koa) and also can be used as standalone. The middleware adds the `parameters` object to the [Express request](http://expressjs.com/4x/api.html#req) (or `ctx.parameters` for [Koa context](http://koajs.com/#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`. | ||
@@ -35,3 +35,3 @@ ## Notice | ||
var params = require('strong-params') | ||
var app = koa() | ||
var app = new koa() | ||
app.use(params.koaMiddleware()) | ||
@@ -55,4 +55,4 @@ ``` | ||
```js | ||
app.use(function *() { | ||
var params = this.params | ||
app.use(function (ctx, next) { | ||
var params = ctx.parameters | ||
}) | ||
@@ -59,0 +59,0 @@ ``` |
@@ -51,7 +51,5 @@ /* global describe, beforeEach, afterEach, it */ | ||
}) | ||
}) | ||
describe('req.parameters.require()', function () { | ||
it('should return a `params` object of the required key', function (done) { | ||
@@ -58,0 +56,0 @@ ctx.app.use(function (req, res, next) { |
@@ -18,5 +18,9 @@ /* global describe, beforeEach, afterEach, it */ | ||
ctx.url = 'http://localhost:' + ctx.port | ||
ctx.app = koa() | ||
ctx.app = new koa() // eslint-disable-line new-cap | ||
qs(ctx.app) | ||
ctx.app.use(bodyparser()) | ||
ctx.app.use(function (ctx, next) { | ||
ctx.params = { id: 'id' } | ||
return next() | ||
}) | ||
ctx.app.use(params.koaMiddleware()) | ||
@@ -29,7 +33,6 @@ }) | ||
describe('req.params.all()', function () { | ||
describe('req.parameters.all()', function () { | ||
it('should return `all` params', function (done) { | ||
ctx.app.use(function *() { | ||
this.body = this.params.all() | ||
ctx.app.use(function (ctx, next) { | ||
ctx.body = ctx.parameters.all() | ||
}) | ||
@@ -39,14 +42,12 @@ ctx.server = ctx.app.listen(ctx.port) | ||
request.post({ url: ctx.url + '/?p1=1&p2=2', form: { a1: 1, a2: 2 } }, function (err, res, body) { | ||
should(JSON.parse(body)).eql({ p1: '1', p2: '2', a1: '1', a2: '2' }) | ||
should(JSON.parse(body)).eql({ p1: '1', p2: '2', a1: '1', a2: '2', id: 'id' }) | ||
done(err) | ||
}) | ||
}) | ||
}) | ||
describe('req.params.permit()', function () { | ||
describe('req.parameters.permit()', function () { | ||
it('should return `permit` selected params', function (done) { | ||
ctx.app.use(function *() { | ||
this.body = this.params.permit('p1', 'a2').value() | ||
ctx.app.use(function (ctx, next) { | ||
ctx.body = ctx.parameters.permit('p1', 'a2').value() | ||
}) | ||
@@ -60,10 +61,8 @@ ctx.server = ctx.app.listen(ctx.port) | ||
}) | ||
}) | ||
describe('req.params.require()', function () { | ||
describe('req.parameters.require()', function () { | ||
it('should return a `params` object of the required key', function (done) { | ||
ctx.app.use(function *() { | ||
this.body = this.params.require('p1').all() | ||
ctx.app.use(function (ctx, next) { | ||
ctx.body = ctx.parameters.require('p1').all() | ||
}) | ||
@@ -79,12 +78,12 @@ ctx.server = ctx.app.listen(ctx.port) | ||
it('should throw an exception if the required key does not exist', function (done) { | ||
ctx.app.use(function * (next) { | ||
ctx.app.use(async function (ctx, next) { | ||
try { | ||
yield* next | ||
await next() | ||
} catch (err) { | ||
this.response.status = 500 | ||
this.response.body = err.message | ||
ctx.response.status = 500 | ||
ctx.response.body = err.message | ||
} | ||
}) | ||
ctx.app.use(function *() { | ||
this.body = this.params.require('xx').all() | ||
ctx.app.use(function (ctx, next) { | ||
ctx.body = ctx.parameters.require('xx').all() | ||
}) | ||
@@ -99,5 +98,3 @@ ctx.server = ctx.app.listen(ctx.port) | ||
}) | ||
}) | ||
}) |
@@ -12,5 +12,3 @@ /* global beforeEach, describe, it */ | ||
describe('Parameters', function () { | ||
describe('class methods', function () { | ||
describe('_initValue', function () { | ||
@@ -109,3 +107,2 @@ PRIMITIVE_TYPES.forEach(function (Primitive) { | ||
}) | ||
}) | ||
@@ -136,3 +133,2 @@ | ||
describe('operations', function () { | ||
describe('constructing', function () {}) | ||
@@ -317,6 +313,13 @@ describe('cloning', function () {}) | ||
}) | ||
it('should handle scalar input for object filter', function () { | ||
// Prepare | ||
var filters = [{'primString': []}] | ||
// Test | ||
var result = params.permit(filters).value() | ||
// Verify | ||
result.should.deepEqual({}) | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
31388
823
0
17
+ Addedlodash@4.17.21(transitive)
- Removedlodash@3.10.1(transitive)
Updatedlodash@^4.17.4