-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(User Validation): validate user signup and login
- 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
Showing
6 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ package-lock.json | |
# test build | ||
.nyc_output | ||
coverage | ||
|
||
.editorconfig |
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,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); | ||
}); | ||
}; |
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,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; |
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,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; |
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 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 | ||
}; |