Skip to content

Commit

Permalink
feature(User Validation): validate user signup and login
Browse files Browse the repository at this point in the history
- install @hapi/joi and express-joi-validator
- validate user details
- customize validation error message
- create user schema
- create validator middleware

[Maintains #167727726]
  • Loading branch information
tobslob committed Aug 21, 2019
1 parent cb5228c commit 7bccd32
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ package-lock.json
# test build
.nyc_output
coverage

.editorconfig
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/register": "^7.5.5",
"@babel/runtime": "^7.5.5",
"bcrypt": "^3.0.6",
"@hapi/joi": "^15.1.1",
"cors": "^2.8.5",
"debug": "^4.1.1",
"dotenv": "^8.0.0",
Expand Down
16 changes: 16 additions & 0 deletions src/middlewares/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Joi from '@hapi/joi';
import response from '../utils/response';

export default (schema) => (req, res, next) => {
if (!schema) return next();

const { body, params, query } = req;

Joi.validate({ ...body, ...params, ...query }, schema, {
abortEarly: false
}).then(() => next())
.catch((err) => {
const errors = err.details.map((e) => e.message);
return response(res, 400, 'error', errors);
});
};
6 changes: 6 additions & 0 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable no-useless-escape */
const messages = {
welcome: 'Welcome to Barefoot Nomad',
apiV1Welcome: 'Welcome to Barefoot Nomad API (version 1)',
notFound: 'Sorry, we cannot find this endpoint',
validEmail: 'Enter a valid email address',
validName: 'Name must be alphabet without number',
validPassword: 'Minimum of 6 letters, a character and number required',
label: 'error',
joiError: 'child \"body\" fails because [child \"error\" fails because ',
};

export default messages;
22 changes: 22 additions & 0 deletions src/validation/JoiValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Joi from '@hapi/joi';

/** *
* Object that help to validate user details
*/
const JoiValidation = {

validateString() {
return Joi.string();
},

validateEmail() {
return Joi.string().email();
},

validatePassword() {
return Joi.string().min(8).strict()
.required();
},
};

export default JoiValidation;
21 changes: 21 additions & 0 deletions src/validation/userSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Joi from '@hapi/joi';
import JoiValidator from './JoiValidator';


const signUpSchema = Joi.object({
email: JoiValidator.validateEmail().required(),
password: JoiValidator.validatePassword().required(),
firstName: JoiValidator.validateString().required(),
lastName: JoiValidator.validateString().required(),
phoneNo: JoiValidator.validateString().required(),
});


const signInSchema = Joi.object({
email: JoiValidator.validateEmail().required(),
password: JoiValidator.validatePassword().required(),
});

export {
signUpSchema, signInSchema
};

0 comments on commit 7bccd32

Please sign in to comment.