Skip to content

Commit 811b414

Browse files
committed
fix detecting false as empty fields
1 parent aa9ffc2 commit 811b414

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

app.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,41 @@ const emptyInputCheck = ({
113113
return;
114114
}
115115

116-
//Array for storing non-0 values
116+
// Array for storing non-0 values
117117
let checkZ = [];
118+
// Array for storing 0s
118119
let zArray = [];
120+
// Array for storing falses
121+
let fArray = [];
122+
// Array for storing the keys
119123
let KeyArray = [];
120124

121125
//Extract the value from the request body
122126
for (const key in body) {
123127

124128
const element = body[key];
125129

126-
if (element !== 0) {
130+
if (element !== 0 && element !== false) {
127131
checkZ.push(element);
128132
KeyArray.push(key);
129133
}
130134

135+
if (element === false) {
136+
fArray.push(element);
137+
}
138+
131139
if (element === 0) {
132140
zArray.push(element);
133141
}
134142
}
135143

136-
//Make sure the array is not empty (0 counts as a valid value)
137-
if (checkZ.length === 0 && zArray.length === 0) {
144+
//Make sure the array is not empty (0 & false count as valid values)
145+
if (checkZ.length === 0 && zArray.length === 0 && fArray.length === 0) {
138146
res.status(400).json({ msg: emptyBodyMsg, });
139147
return;
140148
}
141149

142-
//Check for falsy values in the array without 0s
150+
//Check for falsy values in the array without 0s and falses
143151
for (let index = 0; index < checkZ.length; index++) {
144152
const nonZinput = checkZ[index];
145153
const nonZinputKey = KeyArray[index];

test/main.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ describe('Testing the Input Validation Module', () => {
144144
});
145145
});
146146

147+
test('Sending POST Request with a Body that Contains a Field with Value false', () => {
148+
request.method = 'POST';
149+
request.data.testField = false;
150+
expect.assertions(1);
151+
return axios(request)
152+
.then(data => {
153+
const resBd = data.data.msg;
154+
expect(resBd).toBe(false);
155+
});
156+
});
157+
147158
test('Sending POST Request with an Empty Body', () => {
148159

149160
request.method = 'POST';

test/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//Dependencies
22
const express = require('express');
3+
const axios = require('axios').default;
34
const bodyParser = require('body-parser');
45
const { routeCheck, emptyInputCheck, } = require('../app');
56

@@ -15,6 +16,10 @@ app.use(bodyParser.json({
1516
extended: true,
1617
}));
1718

19+
app.use(bodyParser.urlencoded({
20+
limit: '5mb',
21+
extended: true,
22+
}));
1823
//Headers Settings
1924
app.all('*', (req, res, next) => {
2025
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5003');
@@ -53,7 +58,6 @@ const options = { redirectPath: '/PnF', };
5358
//The routeCheck middleware
5459
app.use(routeCheck(app, options));
5560

56-
5761
const server = app.listen(PORT);
5862

5963
module.exports = { server, };

0 commit comments

Comments
 (0)