Skip to content

Commit

Permalink
created validation method for user signup
Browse files Browse the repository at this point in the history
  • Loading branch information
byohannes committed Sep 24, 2020
1 parent dcbd926 commit 327dd48
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions validator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exports.userSignupValidator = (req, res, next) => {
req.check("name", "Name is required").notEmpty();
req
.check("email", "Email must be between 3 to 32 characters")
.matches(/.+\@.+\..+/)
.withMessage("Email must contain @")
.isLength({
min: 4,
max: 32,
});
req.check("password", "Password is required").notEmpty();
req
.check("password")
.isLength({ min: 6 })
.withMessage("Password must contain at least 6 characters")
.matches(/\d/)
.withMessage("Password must contain a number");
const errors = req.validationErrors();
if (errors) {
const firstError = errors.map((error) => error.msg)[0];
return res.status(400).json({ error: firstError });
}
next();
};

0 comments on commit 327dd48

Please sign in to comment.