-
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.
- Loading branch information
Showing
9 changed files
with
440 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -43,3 +43,6 @@ dist | |
|
||
#coverage file | ||
lcov.info | ||
|
||
#misc | ||
:memory |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { serverResponse, serverError } from './serverResponse'; | ||
import { checkEmail, checkUserName } from './checkExistingUser'; | ||
import { setCustomMessage, validateInputs } from './validationHelper'; | ||
|
||
export { | ||
serverResponse, serverError, checkEmail, checkUserName | ||
serverResponse, | ||
serverError, | ||
checkEmail, | ||
checkUserName, | ||
setCustomMessage, | ||
validateInputs | ||
}; |
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,61 @@ | ||
/* eslint-disable max-len */ | ||
import { serverResponse } from './serverResponse'; | ||
|
||
/** | ||
* Sets and returns custom validation error message | ||
* | ||
* @name setCustomMessage | ||
* @param {string} label - Label of the field | ||
* | ||
* @returns {array} Contains Joi error object | ||
*/ | ||
const setCustomMessage = label => (errors) => { | ||
errors.forEach((err) => { | ||
switch (err.type) { | ||
case 'string.alphanum': | ||
err.message = `${label} should contain only letters and numbers`; | ||
break; | ||
case 'string.min': | ||
err.message = `${label} should not be less than ${err.context.limit} characters`; | ||
break; | ||
case 'string.max': | ||
err.message = `${label} should not be more than ${err.context.limit} characters`; | ||
break; | ||
case 'string.regex.base': | ||
err.message = `${label} is invalid`; | ||
break; | ||
case 'string.regex.invert.base': | ||
err.message = `${label} should not contain spaces`; | ||
break; | ||
default: | ||
err.message = `${label} is required`; | ||
break; | ||
} | ||
}); | ||
|
||
return errors; | ||
}; | ||
|
||
/** | ||
* Validates inputs against a schema | ||
* | ||
* @name validateInput | ||
* @param {object} res - ExpressJs response object | ||
* @param {function} next - ExpressJs next function | ||
* @returns {(JSON|function)} HTTP JSON response or ExpressJs next function | ||
*/ | ||
const validateInputs = (res, next) => (errors) => { | ||
const validationErrors = {}; | ||
|
||
if (!errors) return next(); | ||
|
||
errors.details.forEach((error) => { | ||
const errorClone = { ...error }; | ||
const { key } = errorClone.context; | ||
validationErrors[key] = errorClone.message.replace(/"/g, ''); | ||
}); | ||
|
||
serverResponse(res, 422, { errors: validationErrors }); | ||
}; | ||
|
||
export { setCustomMessage, validateInputs }; |
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,21 @@ | ||
import Joi from '@hapi/joi'; | ||
import userSignupSchema from '../schemas/userSignup'; | ||
import { validateInputs } from '../helpers'; | ||
|
||
/** | ||
* Validates user registration/signup input | ||
* | ||
* @param {string} req - ExpressJs request object | ||
* @param {string} res - ExpressJs response object | ||
* @param {string} next - ExpressJs next function | ||
* @returns {(JSON|function)} HTTP JSON response or ExpressJs next function | ||
*/ | ||
const validateUserSignup = (req, res, next) => { | ||
const options = { | ||
abortEarly: false | ||
}; | ||
|
||
Joi.validate(req.body, userSignupSchema, options, validateInputs(res, next)); | ||
}; | ||
|
||
export default validateUserSignup; |
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,35 @@ | ||
import Joi from '@hapi/joi'; | ||
import { setCustomMessage } from '../helpers'; | ||
|
||
export default { | ||
firstName: Joi.string() | ||
.required() | ||
.min(2) | ||
.max(50) | ||
.regex(/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/) | ||
.error(setCustomMessage('first name')), | ||
lastName: Joi.string() | ||
.required() | ||
.min(2) | ||
.max(50) | ||
.regex(/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/) | ||
.error(setCustomMessage('last name')), | ||
userName: Joi.string() | ||
.required() | ||
.min(6) | ||
.max(15) | ||
.alphanum() | ||
.error(setCustomMessage('username')), | ||
email: Joi.string() | ||
.required() | ||
.min(3) | ||
.max(254) | ||
.regex(/^[\w._]+@[\w]+[-.]?[\w]+\.[\w]+$/) | ||
.error(setCustomMessage('email')), | ||
password: Joi.string() | ||
.required() | ||
.min(8) | ||
.max(254) | ||
.regex(/\s/, { invert: true }) | ||
.error(setCustomMessage('password')) | ||
}; |
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
Oops, something went wrong.