Skip to content

Commit

Permalink
Merge pull request #35 from olusoladavid/ch-refactor-validator-159732588
Browse files Browse the repository at this point in the history
#159732588 Refactor validation handlers into single middleware
  • Loading branch information
olusoladavid committed Aug 14, 2018
2 parents 14d25fb + 08dacf6 commit 44e0e0f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
16 changes: 0 additions & 16 deletions server/controllers/entryController.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions server/utils/handleValidationErrors.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 44e0e0f

Please sign in to comment.