Skip to content

Instantly share code, notes, and snippets.

@ChangJoo-Park
Last active May 17, 2018 05:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChangJoo-Park/ca211c7b117d9ca91a3b3c1cec0c9342 to your computer and use it in GitHub Desktop.
Save ChangJoo-Park/ca211c7b117d9ca91a3b3c1cec0c9342 to your computer and use it in GitHub Desktop.
Strong Parameter for Express
exports.parameterPermitter = function ({ params = [] }) {
if (!Array.isArray(params)) throw new Error(`Request Parameter ${params} is invalid parameters array`)
return function (req, res, next) {
res.locals.permitted = {}
params.forEach((param) => {
if (!req.body.hasOwnProperty(param)) throw new Error(`${param} is required`)
res.locals.permitted[param] = req.body[param]
})
next()
}
}
/**
* Parameter Permitter Example
*/
/*
router.post('/', paramsPermitter({
params: ['world']
}), function (req, res, next) {
res.json(res.locals.permitted)
});
*/
@ChangJoo-Park
Copy link
Author

ChangJoo-Park commented May 16, 2018

middleware strong-params recommended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment