Skip to content

Commit 7209605

Browse files
committed
return the missing field key
1 parent 5de6772 commit 7209605

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,33 @@ Note: 0 does count as a valid input.
6262

6363
> Default Error Messages:
6464
65-
1. Requests with an empty body will be met with a **400** and the message: `{ msg: 'The request body is empty!'}`.
65+
1. Requests with an empty body will be met with a **400** and the message:
6666

67-
2. Request with empty fields in the body will be met with a **400** and the message: `{msg:'Some fields are missing!'}`.
67+
```javascript
68+
{
69+
msg: 'The request body is empty!';
70+
}
71+
```
72+
73+
2. Request with empty fields in the body will be met with a **400** and the message:
74+
75+
```javascript
76+
{
77+
msg: 'The request body is empty!',
78+
field:'key of the missing field'
79+
}
80+
```
81+
82+
```javascript
83+
//supressFieldKey=true
84+
{
85+
msg: 'The request body is empty!',
86+
}
87+
```
6888

6989
You can specify the middleware to skip the check for **GET** requests in the config option.
7090

71-
Custom error message for each scenario above can also be set like the example below:
91+
Custom error message for each scenario above and whether to supress the filed key can be specified like the example below:
7292

7393
```javascript
7494
//Dependencies
@@ -97,6 +117,7 @@ app.use(
97117
checkGet: true, //Wether to check the GET requests or not
98118
emptyBodyMsg: 'Err Msg 1', //Custom msg for empty body
99119
emptyFieldMsg: 'Err Msg 2', //Custom msg for missing fields
120+
supressFieldKey = false, //To supress the key of the missing field
100121
}),
101122
);
102123

app.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const emptyInputCheck = ({
8484
checkGet = true,
8585
emptyBodyMsg = 'The request body is empty!',
8686
emptyFieldMsg = 'Some fields are missing!',
87+
supressFieldKey = false,
8788
} = {}) => {
8889

8990
return (req, res, next) => {
@@ -102,7 +103,6 @@ const emptyInputCheck = ({
102103
}
103104
}
104105

105-
106106
//Deconstruct the request to get the body out
107107
const { body, } = req;
108108

@@ -116,6 +116,7 @@ const emptyInputCheck = ({
116116
//Array for storing non-0 values
117117
let checkZ = [];
118118
let zArray = [];
119+
let KeyArray = [];
119120

120121
//Extract the value from the request body
121122
for (const key in body) {
@@ -124,6 +125,7 @@ const emptyInputCheck = ({
124125

125126
if (element !== 0) {
126127
checkZ.push(element);
128+
KeyArray.push(key);
127129
}
128130

129131
if (element === 0) {
@@ -140,14 +142,26 @@ const emptyInputCheck = ({
140142
//Check for falsy values in the array without 0s
141143
for (let index = 0; index < checkZ.length; index++) {
142144
const nonZinput = checkZ[index];
145+
const nonZinputKey = KeyArray[index];
143146

144147
//Return false if there is any falsy value
145-
if (!nonZinput) {
148+
if (!nonZinput && supressFieldKey === false) {
149+
res.status(400).json({
150+
msg: emptyFieldMsg,
151+
field: nonZinputKey,
152+
});
153+
return;
154+
}
146155

147-
res.status(400).json({ msg: emptyFieldMsg, });
156+
//Supress field key
157+
if (!nonZinput) {
158+
res.status(400).json({
159+
msg: emptyFieldMsg,
160+
});
148161
return;
149162
}
150163

164+
151165
}
152166

153167
return next();

test/main.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ describe('Testing the Input Validation Module', () => {
5757
});
5858
});
5959

60-
6160
const request = {
6261
method: '',
6362
url: 'http://localhost:5003/',
@@ -109,11 +108,13 @@ describe('Testing the Input Validation Module', () => {
109108
testField: 'test_data',
110109
testField1: '',
111110
};
112-
expect.assertions(1);
111+
expect.assertions(2);
113112
return axios(request)
114113
.catch(err => {
115114
const errMsg = err.response.data.msg;
115+
const missingField = err.response.data.field;
116116
expect(errMsg).toBe('Some fields are missing!');
117+
expect(missingField).toBe('testField1');
117118
});
118119
});
119120
});
@@ -162,11 +163,13 @@ describe('Testing the Input Validation Module', () => {
162163
testField: 'test_data',
163164
testField1: '',
164165
};
165-
expect.assertions(1);
166+
expect.assertions(2);
166167
return axios(request)
167168
.catch(err => {
168169
const errMsg = err.response.data.msg;
170+
const missingField = err.response.data.field;
169171
expect(errMsg).toBe('Some fields are missing!');
172+
expect(missingField).toBe('testField1');
170173
});
171174
});
172175
});

0 commit comments

Comments
 (0)