generated from Arquisoft/wiq_0
-
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.
- Loading branch information
1 parent
b36cc0b
commit 6ae9154
Showing
8 changed files
with
166 additions
and
5 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
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,90 @@ | ||
import { body, param, validationResult } from 'express-validator'; | ||
import { BadRequestError } from '../errors/customErrors.js'; | ||
import User from '../user-model.js'; | ||
|
||
const withValidationErrors = (validateValues) => { | ||
return [ | ||
validateValues, | ||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
const errorMessages = errors.array().map((error) => error.msg); | ||
throw new BadRequestError(errorMessages); | ||
} | ||
next(); | ||
}, | ||
]; | ||
}; | ||
|
||
export const validateRegisterInput = withValidationErrors([ | ||
body('name') | ||
.notEmpty() | ||
.withMessage('name is required') | ||
.isLength({ max: 20 }) | ||
.withMessage('name must be no more than 20 characters long'), | ||
body('lastName') | ||
.notEmpty() | ||
.withMessage('last name is required') | ||
.isLength({ max: 20 }) | ||
.withMessage('last name must be no more than 20 characters long'), | ||
body('username') | ||
.notEmpty() | ||
.withMessage('username is required') | ||
.isLength({ max: 20 }) | ||
.withMessage('username must be no more than 20 characters long'), | ||
body('email') | ||
.notEmpty() | ||
.withMessage('email is required') | ||
.isLength({ max: 40 }) | ||
.withMessage('email must be no more than 20 characters long') | ||
.isEmail() | ||
.withMessage('invalid email format') | ||
.isLength({ max: 40 }) | ||
.withMessage('email must be no more than 20 characters long') | ||
.custom(async (email) => { | ||
const user = await User.findOne({ email }); | ||
if (user) throw new BadRequestError('email already exists'); | ||
}), | ||
body('password') | ||
.notEmpty() | ||
.withMessage('password is required') | ||
.isLength({ min: 8 }) | ||
.withMessage('password must be at least 8 characters long') | ||
.isLength({ max: 20 }) | ||
.withMessage('password must be no more than 20 characters long'), | ||
]); | ||
|
||
export const validateUpdateUserInput = withValidationErrors([ | ||
body('name') | ||
.notEmpty() | ||
.withMessage('name is required') | ||
.isLength({ max: 20 }) | ||
.withMessage('name must be no more than 20 characters long'), | ||
body('lastName') | ||
.notEmpty() | ||
.withMessage('last name is required') | ||
.isLength({ max: 40 }) | ||
.withMessage('last name must be no more than 20 characters long'), | ||
body('username') | ||
.notEmpty() | ||
.withMessage('username is required') | ||
.isLength({ max: 20 }) | ||
.withMessage('username must be no more than 20 characters long'), | ||
body('email') | ||
.notEmpty() | ||
.withMessage('email is required') | ||
.isLength({ max: 40 }) | ||
.withMessage('email must be no more than 20 characters long') | ||
.isEmail() | ||
.withMessage('invalid email format') | ||
.isLength({ max: 40 }) | ||
.withMessage('email must be no more than 20 characters long') | ||
.custom(async (email, { req }) => { | ||
const user = await User.findOne({ email }); | ||
console.log(user); | ||
console.log(req.user); | ||
if (user && user._id.toString() !== req.user.userId) { | ||
throw new BadRequestError('email already exists'); | ||
} | ||
}), | ||
]); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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