Last active
May 17, 2018 05:47
-
-
Save ChangJoo-Park/ca211c7b117d9ca91a3b3c1cec0c9342 to your computer and use it in GitHub Desktop.
Strong Parameter for Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
middleware strong-params recommended