From 08dacf6e2713a6ad44797cb7303a5c537b8866b1 Mon Sep 17 00:00:00 2001 From: Olusola David Date: Mon, 13 Aug 2018 14:48:26 +0100 Subject: [PATCH] chore(Validation): refactor validation handlers into single middleware - refactor validator into handleValidationErrors.js [Delivers #159732588] --- server/controllers/entryController.js | 16 ---------------- server/controllers/userController.js | 16 +--------------- server/routes/index.js | 9 +++++---- server/utils/handleValidationErrors.js | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 35 deletions(-) create mode 100644 server/utils/handleValidationErrors.js diff --git a/server/controllers/entryController.js b/server/controllers/entryController.js index f450ca9..f344cb5 100644 --- a/server/controllers/entryController.js +++ b/server/controllers/entryController.js @@ -1,4 +1,3 @@ -import { validationResult } from 'express-validator/check'; import { query } from '../db/index'; import validate from '../utils/validate'; import queries from '../db/queries'; @@ -42,13 +41,6 @@ class entryController { * @memberof entryController */ static addEntry(req, res) { - // validate entry fields - 400 - const errorsFound = validationResult(req); - if (!errorsFound.isEmpty()) { - res.status(400).json({ error: { message: errorsFound.array() } }); - return; - } - const { title, content, is_favorite: isFavorite } = req.body; // add entry to database @@ -100,18 +92,10 @@ class entryController { * @memberof entryController */ static modifyEntry(req, res) { - // validate entry fields - 400 - const errorsFound = validationResult(req); - if (!errorsFound.isEmpty()) { - res.status(400).json({ error: { message: errorsFound.array() } }); - return; - } - // deconstruct request body let { title, content } = req.body; title = title || null; content = content || null; - // check if isFavorite is defined const isFavorite = validate.booleanOrNull(req.body.is_favorite); // check if entry is already older than a day diff --git a/server/controllers/userController.js b/server/controllers/userController.js index 5122e77..ac98e82 100644 --- a/server/controllers/userController.js +++ b/server/controllers/userController.js @@ -1,10 +1,8 @@ -import { validationResult } from 'express-validator/check'; import bcrypt from 'bcrypt'; import { query } from '../db/index'; import queries from '../db/queries'; import signAuthToken from '../utils/signAuthToken'; - class userController { /** * Creates a new user account @@ -19,13 +17,6 @@ class userController { // get email and password in request body const { email, password } = req.body; - // validate email and password - 400 - const errorsFound = validationResult(req); - if (!errorsFound.isEmpty()) { - res.status(400).json({ error: { message: errorsFound.array() } }); - return; - } - // check if email already exists - 409 query(queries.getOneUser, [email], (qErr, user) => { if (qErr) { @@ -75,12 +66,7 @@ class userController { static loginUser(req, res) { // get email and password in request body const { email, password } = req.body; - // validate email and password - 400 - const errorsFound = validationResult(req); - if (!errorsFound.isEmpty()) { - res.status(400).json({ error: { message: errorsFound.array() } }); - return; - } + // fetch user query(queries.getOneUser, [email], (qErr, userData) => { if (qErr) { diff --git a/server/routes/index.js b/server/routes/index.js index cb9e61d..c2afbac 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -3,6 +3,7 @@ import userController from '../controllers/userController'; import entryController from '../controllers/entryController'; import verifyToken from '../utils/verifyToken'; import validate from '../utils/validate'; +import handleValidationErrors from '../utils/handleValidationErrors'; const router = express.Router(); @@ -12,22 +13,22 @@ router.get('/', (req, res) => { }); /* Create a new user */ -router.post('/auth/signup', validate.signupInputs, userController.createUser); +router.post('/auth/signup', validate.signupInputs, handleValidationErrors, userController.createUser); /* Login user */ -router.post('/auth/login', validate.loginInputs, userController.loginUser); +router.post('/auth/login', validate.loginInputs, handleValidationErrors, userController.loginUser); /* GET all user entries */ router.get('/entries', verifyToken, entryController.getAllEntries); /* POST a new entry */ -router.post('/entries', verifyToken, validate.newEntry, entryController.addEntry); +router.post('/entries', verifyToken, validate.newEntry, handleValidationErrors, entryController.addEntry); /* GET a single entry */ router.get('/entries/:id', verifyToken, entryController.getEntry); /* PUT new data in existing entry */ -router.put('/entries/:id', verifyToken, validate.modifyEntry, entryController.modifyEntry); +router.put('/entries/:id', verifyToken, validate.modifyEntry, handleValidationErrors, entryController.modifyEntry); /* DELETE a single entry */ router.delete('/entries/:id', verifyToken, entryController.deleteEntry); diff --git a/server/utils/handleValidationErrors.js b/server/utils/handleValidationErrors.js new file mode 100644 index 0000000..03f63d4 --- /dev/null +++ b/server/utils/handleValidationErrors.js @@ -0,0 +1,16 @@ +import { validationResult } from 'express-validator/check'; + +const handleValidationErrors = (req, res, next) => { + const errors = validationResult(req); + if (errors.isEmpty()) { + next(); + } else { + const messages = errors.array().map((error) => { + const { param, msg: message } = error; + return { param, message }; + }); + res.status(400).json({ error: messages }); + } +}; + +export default handleValidationErrors;