Skip to content

Commit

Permalink
Refactor validateinput.js return messages to json object
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenezerdon committed Nov 21, 2018
1 parent 152dfb2 commit 6ca0268
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions UI/assets/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const loginUser = (e) => {
window.location.replace('./pages/attendantdashboard.html');
}
snackbar(data.message);
console.log(data);
})
.catch((err) => {
console.log(err);
Expand Down
40 changes: 24 additions & 16 deletions server/routes/middleware/validateinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ const validateUserInput = (req, res, next) => {
} = req;

if (!String(body.emailAddress)) {
return (
res.status(400).json('The user\'s email adress has to be a string!')
);
return res.status(400).json({
message: 'The user\'s email adress has to be a string!',
success: false,
});
}
if (body.password.length < 6) {
return (
res.status(400).json('The user\'s password has to be more than 6 characters!')
);
return res.status(400).json({
message: 'The user\'s password has to be more than 6 characters!',
success: false,
});
}
return next();
};

const validateId = (req, res, next) => {
if (!Number(req.params.id)) {
return res.status(401).json('Hi! The id has to be a number');
return res.status(400).json({
message: 'Hi! The id has to be a number',
success: false,
});
}
return next();
};
Expand All @@ -32,9 +37,10 @@ const validateUserSignup = (req, res, next) => {
});
}
if (!String(body.fullname)) {
return (
res.status(400).json('The user\'s name has to be a string!')
);
return res.status(400).json({
message: 'The user\'s name has to be a string!',
success: false,
});
}
if (!body.fullname || !body.emailaddress
|| !body.role || !body.password) {
Expand All @@ -51,9 +57,10 @@ const validateProductInput = (req, res, next) => {
body,
} = req;
if (!body.productname || !body.description || !body.price || !body.quantity || !body.minallowed) {
return (
res.status(400).json('Hi! Some details are missing. Can you check and try again?')
);
return res.status(400).json({
message: 'Some details are missing. Can you check and try again?',
success: false,
});
}
return next();
};
Expand All @@ -63,9 +70,10 @@ const validateSaleInput = (req, res, next) => {
body,
} = req;
if (!body.productname || !body.price || !body.quantity) {
return (
res.status(400).json('Hi! Some details are missing. Can you check and try again?')
);
return res.status(400).json({
message: 'Some details are missing. Can you check and try again?',
success: false,
});
}
return next();
};
Expand Down

0 comments on commit 6ca0268

Please sign in to comment.