Last active
January 25, 2021 20:11
-
-
Save singh1114/766113f61c4f61b8357e4ff0223ec3c1 to your computer and use it in GitHub Desktop.
Node.js middleware article
This file contains 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
const validateParams = function (requestParams) { | |
return function (req, res, next) { | |
for (let param of requestParams) { | |
if (checkParamPresent(Object.keys(req.body), param)) { | |
let reqParam = req.body[param.param_key]; | |
if (!checkParamType(reqParam, param)) { | |
return res.send(400, { | |
status: 400, | |
result: `${param.param_key} is of type ` + | |
`${typeof reqParam} but should be ${param.type}` | |
}); | |
} else { | |
if (!runValidators(reqParam, param)) { | |
return res.send(400, { | |
status: 400, | |
result: `Validation failed for ${param.param_key}` | |
}); | |
} | |
} | |
} else if (param.required){ | |
return res.send(400, { | |
status: 400, | |
result: `Missing Parameter ${param.param_key}` | |
}); | |
} | |
} | |
next(); | |
} | |
}; | |
const checkParamPresent = function (reqParams, paramObj) { | |
return (reqParams.includes(paramObj.param_key)); | |
}; | |
const checkParamType = function (reqParam, paramObj) { | |
const reqParamType = typeof reqParam; | |
return reqParamType === paramObj.type; | |
}; | |
const runValidators = function (reqParam, paramObj) { | |
for (let validator of paramObj.validator_functions) { | |
if (!validator(reqParam)) { | |
return false | |
} | |
} | |
return true; | |
}; | |
module.exports = { | |
validateParams: validateParams | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the appreciation @motdde