-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created validation method for user signup
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
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
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(); | ||
}; |