I am currently creating an API for posting in backend with nodejs. My data object, which gets provided from the frontend contains multiple objects.
My first step is to check if these multiple consts are empty and if they are, they give specific error messages, which looks like this:
//constants
const { name, lang, email } = req.body;
if (!name) {
return res.status(400).json({
error_code: 1,
message: 'Missing name',
});
}
if (!lang) {
return res.status(400).json({
error_code: 2,
message: 'Lang not provided',
});
}
if (!email) {
return res.status(400).json({
error_code: 3,
message: 'email is not provided',
});
}
In the example above there are only three parameters but how would it look for much more constants?
I thought about doing a switch ... case but I am not sure if that is the best solution and how exactly that would look like.
Do you guys have any experience or suggestions?
messagefor each missing/invalid/... property, or do you also need a distintiveerror_code? What result do you expect if multiple properties are invalid/missing?