forked from hagopj13/node-express-boilerplate
-
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
Showing
7 changed files
with
87 additions
and
2 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
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,19 @@ | ||
const Joi = require('@hapi/joi'); | ||
const httpStatus = require('http-status'); | ||
const { pick } = require('lodash'); | ||
const AppError = require('../utils/AppError'); | ||
|
||
const validate = schema => (req, res, next) => { | ||
const validSchema = pick(schema, ['params', 'query', 'body']); | ||
const toValidate = pick(req, Object.keys(validSchema)); | ||
const { value, error } = Joi.compile(validSchema).validate(toValidate, { abortEarly: true }); | ||
|
||
if (error) { | ||
const errorMessage = error.details.map(details => details.message).join(', '); | ||
return next(new AppError(httpStatus.BAD_REQUEST, errorMessage)); | ||
} | ||
Object.assign(req, value); | ||
return next(); | ||
}; | ||
|
||
module.exports = validate; |
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,8 +1,10 @@ | ||
const express = require('express'); | ||
const validate = require('../../middlewares/validate'); | ||
const authValidation = require('../../validations/auth.validation'); | ||
const authController = require('../../controllers/auth.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/register', authController.register); | ||
router.post('/register', validate(authValidation.register), authController.register); | ||
|
||
module.exports = router; |
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,23 @@ | ||
const Joi = require('@hapi/joi'); | ||
|
||
const register = { | ||
body: Joi.object().keys({ | ||
email: Joi.string() | ||
.required() | ||
.email(), | ||
password: Joi.string() | ||
.required() | ||
.min(8) | ||
.custom((value, helpers) => { | ||
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) { | ||
return helpers.message('Password must contain at least one letter and one number'); | ||
} | ||
return value; | ||
}), | ||
name: Joi.string().required(), | ||
}), | ||
}; | ||
|
||
module.exports = { | ||
register, | ||
}; |
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 @@ | ||
module.exports.authValidation = require('./auth.validation'); |
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