Skip to content

Commit

Permalink
Implementing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fejiroofficial committed Apr 22, 2019
1 parent f0ee65c commit e491e66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
10 changes: 10 additions & 0 deletions server/helpers/regex.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
export const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/;
export const phoneRegex = /^\+(?:[0-9] ?){6,14}[0-9]$/;

export const testInput = (input) => {
const regex = /[a-zA-Z]+.*/;
return regex.test(input);
};

export const testCategoryId = (input) => {
const regexNum = /^[0-9]+$/;
return regexNum.test(input);
};
38 changes: 10 additions & 28 deletions server/middlewares/articlesMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-restricted-globals */
// import createError from 'http-errors';
import slugify from 'slugify';
import uuid from 'uuid/v4';
import jwt from 'jsonwebtoken';
import articleHelpers from '../helpers/articleHelpers';
import Response from '../helpers/responseHelper';
import models from '../models';
import { STATUS, MESSAGE, PAGE_LIMIT } from '../helpers/constants';
import { testInput, testCategoryId } from '../helpers/regex';

/**
* Wrapper class for validating requests.
Expand All @@ -33,32 +32,15 @@ export default class AriclesMiddleware {
} = req.body;


if (!title) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'The title of an article cannot be empty', false);
}
if (!isNaN(title)) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'The title of an article cannot contain only numbers or spaces', false);
}
if (!description) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'Please provide a short description for this article', false);
}
if (!isNaN(description)) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'The description of this article cannot contain only numbers or spaces', false);
}
if (description.length > 255) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'Description cannot be more than 255 characters', false);
}
if (!body) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'Please provide the details for this article', false);
}
if (!isNaN(body)) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'The article detail cannot contain only numbers or spaces', false);
}
if (!categoryId) {
return Response.send(res, STATUS.BAD_REQUEST, [], 'You have not selected a category for this article', false);
}

if (isNaN(categoryId)) {
if (!title) return Response.send(res, STATUS.BAD_REQUEST, [], 'The title of an article cannot be empty', false);
if (!testInput(title)) return Response.send(res, STATUS.BAD_REQUEST, [], 'The title of an article cannot contain only numbers or spaces', false);
if (!description) return Response.send(res, STATUS.BAD_REQUEST, [], 'Please provide a short description for this article', false);
if (!testInput(description)) return Response.send(res, STATUS.BAD_REQUEST, [], 'The description of this article cannot contain only numbers or spaces', false);
if (description.length > 255) return Response.send(res, STATUS.BAD_REQUEST, [], 'Description cannot be more than 255 characters', false);
if (!body) return Response.send(res, STATUS.BAD_REQUEST, [], 'Please provide the details for this article', false);
if (!testInput(body)) return Response.send(res, STATUS.BAD_REQUEST, [], 'The article detail cannot contain only numbers or spaces', false);
if (!categoryId) return Response.send(res, STATUS.BAD_REQUEST, [], 'You have not selected a category for this article', false);
if (!testCategoryId(categoryId)) {
return Response.send(
res, STATUS.BAD_REQUEST, {}, 'category type must be an integer', false,
);
Expand Down

0 comments on commit e491e66

Please sign in to comment.