Skip to content

Commit eae7f45

Browse files
committed
add badge
1 parent 45f6fa1 commit eae7f45

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## A collection of Middlewares for Express
44

5+
[![Maintainability](https://api.codeclimate.com/v1/badges/48a4f566a0ab37a4f5d4/maintainability)](https://codeclimate.com/github/algo7/express-suite/maintainability)
6+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/230d4840e65c45e2bc7682ee4659a7c9)](https://www.codacy.com/manual/algo7/express-suite?utm_source=github.com&utm_medium=referral&utm_content=algo7/express-suite&utm_campaign=Badge_Grade)
7+
[![](https://img.shields.io/github/license/algo7/express-suite)]()
8+
[![](https://img.shields.io/github/issues/algo7/express-suite)]()
9+
[![](https://img.shields.io/github/forks/algo7/express-suite)]()
10+
[![](https://img.shields.io/github/stars/algo7/express-suite)]()
11+
512
## Installation
613

714
```
@@ -48,9 +55,11 @@ app.listen(5003);
4855

4956
The poject uses **Jest** for testing.
5057

51-
First install the dependencies then run `npm test`
58+
Clone the Github repo. first. Install the dependencies then run `npm test`
5259

5360
```
61+
$ git clone https://github.com/algo7/express-suite.git
62+
$ cd express-suite
5463
$ npm i
5564
$ npm test
5665
```

app.js

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Takes in the **express()** function
44
* @param {Object} app - The **express()** function
55
* @param {Object} [opt] - The options
6-
* @param {String} [opt.path='/x'] - The redirect path
6+
* @param {String} [opt.path] - The redirect path
77
* @example //For the option
88
{ path: '/PnF' }
99
*
@@ -76,7 +76,81 @@ const routeCheck = (app, opt) => {
7676

7777
};
7878

79+
//Route Validation Function
80+
/**
81+
*
82+
* @param {Object} [opt] - The options
83+
* @param {Boolean} [opt.checkGet]
84+
* @returns {function():void}
85+
*/
86+
const inputValidation = (opt) => {
87+
88+
89+
return (req, res, next) => {
90+
91+
//Solve cors issue || Browser will send an OPTIONS request
92+
//that expects a HTTP staus code of 200
93+
if (req.method === 'OPTIONS') {
94+
return res.sendStatus(200);
95+
}
96+
97+
//Let the get method pass
98+
if (req.method === 'GET') {
99+
return next();
100+
}
101+
102+
//Deconstruct the request to get the body out
103+
const { body, } = req;
104+
105+
//Check if the input is any flase value
106+
//(when nothing is pass into the middleware)
107+
if (!body) {
108+
109+
res.status(400).json({ msg: 'Some fields are missing!', });
110+
return;
111+
}
112+
113+
//Array for storing non-0 values
114+
let checkZ = [];
115+
116+
for (const key in body) {
117+
118+
const element = body[key];
119+
120+
if (element !== 0) {
121+
checkZ.push(element);
122+
}
123+
124+
}
125+
126+
//Make sure the array is not empty
127+
if (checkZ.length === 0) {
128+
129+
res.status(400).json({ msg: 'Some fields are missing!', });
130+
return;
131+
}
132+
133+
//Check for falsy values in the array without 0s
134+
for (let index = 0; index < checkZ.length; index++) {
135+
const nonZinput = checkZ[index];
136+
137+
//Return false if there is any falsy value
138+
if (!nonZinput) {
139+
140+
res.status(400).json({ msg: 'Some fields are missing!', });
141+
return;
142+
}
143+
144+
}
145+
146+
return next();
147+
148+
};
149+
};
150+
151+
79152

80153
module.exports = {
81154
routeCheck,
155+
inputValidation,
82156
};

0 commit comments

Comments
 (0)