Guest User

Untitled

a guest
May 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. exports.parameterPermitter = function ({ params = [] }) {
  2. if (!Array.isArray(params)) throw new Error(`Request Parameter ${params} is invalid parameters array`)
  3.  
  4.  
  5. return function (req, res, next) {
  6. res.locals.permitted = {}
  7. params.forEach((param) => {
  8. if (!req.body.hasOwnProperty(param)) throw new Error(`${param} is required`)
  9. res.locals.permitted[param] = req.body[param]
  10. })
  11. next()
  12. }
  13. }
  14.  
  15. /**
  16. * Parameter Permitter Example
  17. */
  18. /*
  19. router.post('/', paramsPermitter({
  20. params: ['world']
  21. }), function (req, res, next) {
  22. res.json(res.locals.permitted)
  23. });
  24. */
Add Comment
Please, Sign In to add comment