Skip to content

Commit

Permalink
Merge 4a1b8f0 into a16f016
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulfatai360 committed Aug 1, 2019
2 parents a16f016 + 4a1b8f0 commit d4575a3
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ dist

#coverage file
lcov.info

#misc
:memory
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"@hapi/joi": "^15.1.0",
"coveralls": "^3.0.5",
"debug": "^4.1.1",
"dotenv": "^8.0.0",
Expand Down
8 changes: 7 additions & 1 deletion server/helpers/index.js
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
};
61 changes: 61 additions & 0 deletions server/helpers/validationHelper.js
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 };
21 changes: 21 additions & 0 deletions server/middlewares/userValidation.js
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;
35 changes: 35 additions & 0 deletions server/schemas/userSignup.js
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'))
};
1 change: 1 addition & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai from 'chai';
import chaiHttp from 'chai-http';
import server from '../server';
import './users';
import './middlewares/userValidation.test';

const { expect } = chai;

Expand Down
Loading

0 comments on commit d4575a3

Please sign in to comment.