-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[finishes #167727453]
- Loading branch information
Showing
5 changed files
with
180 additions
and
3 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
Empty file.
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
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
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,57 @@ | ||
import { check, validationResult } from 'express-validator'; | ||
|
||
const customMessage = validationResult.withDefaults({ | ||
formatter: (error) => ({ | ||
field: error.param, | ||
message: error.msg | ||
}) | ||
}); | ||
|
||
const signupValidator = [ | ||
check('first_name', 'Invalid first name.') | ||
.not().isEmpty().withMessage('First name is required') | ||
.isAlpha() | ||
.withMessage('Special characters and digits are not allowed') | ||
.isLength({ | ||
min: 2, | ||
max: 20 | ||
}) | ||
.withMessage('Too short: Enter a miximum of 20 and a minimum of 2 characters.'), | ||
|
||
check('last_name', 'Invalid last name.') | ||
.not().isEmpty().withMessage('Last name is required') | ||
.isAlpha() | ||
.withMessage('Special characters and digits are not allowed') | ||
.isLength({ | ||
min: 2, | ||
max: 20 | ||
}) | ||
.withMessage('Too short: Enter a miximum of 20 and a minimum of 2 characters.'), | ||
|
||
check('email', 'Invalid Email') | ||
.not().isEmpty().withMessage('Email is required') | ||
.isEmail() | ||
.normalizeEmail(), | ||
|
||
check('password', 'Invalid password') | ||
.not().isEmpty().withMessage('Password is required') | ||
.isLength({ min: 6, max: 20 }) | ||
.withMessage('password must be at least 6 characters') | ||
.not() | ||
.matches(/\s/, 'g') | ||
.withMessage('password cannot contain whitespace'), | ||
|
||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
return errors.isEmpty() | ||
? next() | ||
: res.status(422).json({ | ||
status: 'error', | ||
error: customMessage(req).array() | ||
}); | ||
} | ||
|
||
|
||
]; | ||
|
||
export default signupValidator; |