Skip to content

Commit 68a0fd4

Browse files
authored
Merge pull request #11 from gsmithun4/development
Hot Fix
2 parents 7162bc7 + 44ae30e commit 68a0fd4

File tree

4 files changed

+158
-39
lines changed

4 files changed

+158
-39
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,16 @@ Ends a validation definition
300300
{
301301
"field1": "Value", // String, Mandatory
302302
"field2": [ // array, Mandatory
303-
{ "field21": "44443" } // Number Optional
303+
{ "field21": "44443" }, // object Optional, number mandatory
304+
{ "field21": "44443" }
304305
],
305306
"field3": { // Object Optional
306-
{ "field31": "true" }, // Boolean Mandatory
307-
{ "field32": "String" } // String Mandatory
308-
}
307+
"field31": "true", // Boolean Mandatory
308+
"field32": "String" // String Mandatory
309+
},
310+
"field4": [ // array, Mandatory
311+
123, 445, 3434 // Number Optional
312+
],
309313
}
310314
```
311315
Should send http status code 500 in case of error
@@ -315,12 +319,17 @@ router.post('/users/:id',
315319
validateBody().isToBeRejected().sendErrorCode(500).addParams([
316320
param('field1').isRequired().end(),
317321
param('field2').isRequired().isArray().isRequired().addChild(
318-
param('field21').isNumber().end()
322+
param('field2-array').isObject().addChild( // field2-array is for tracking, you can give any name here
323+
param('field21').isNumber().isRequired().end()
324+
).end()
319325
).end(),
320-
param('field3').isRequired().isObject().addChildren([
326+
param('field3').isObject().addChildren([
321327
param('field31').isBoolean().isRequired().end(),
322328
param('field32').isRequired().end()
323329
]).end(),
330+
param('field4').isRequired().isArray().isRequired().addChild(
331+
param('field4-array').isNumber().end()
332+
).end(),
324333
]).done(),
325334
(req, res, next) => {
326335

lib/validator/validator.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = (location = 'body', validation = [], response = {}) => {
3939
return false;
4040
};
4141
const checkFormat = (value, validateObj) => {
42-
const { param, location, isRequired, isNumber, isEmail, isBoolean,
42+
const { param, isRequired, isNumber, isEmail, isBoolean,
4343
length, message, isDate, format, isArray, isObject, range,
4444
includes, excludes, mobileNumber } = validateObj;
4545

@@ -215,21 +215,19 @@ module.exports = (location = 'body', validation = [], response = {}) => {
215215
});
216216
};
217217
const getChildArrayErrors = (child, value) => {
218+
const arrayType = child[0];
218219
return value.map(v => {
219-
return child.map(c => {
220-
const subvalue = getValue(v, c.param);
221-
const err = checkFormat(subvalue, c);
222-
if (err)
223-
return [err];
220+
const err = checkFormat(v, arrayType);
221+
if (err)
222+
return [err];
224223

225-
if (subvalue && c.children) {
226-
if (c.isArray) {
227-
return getChildArrayErrors(c.children, subvalue);
228-
} else if (c.isObject) {
229-
return getChildErrors(c.children, subvalue);
230-
}
224+
if (v && arrayType.children) {
225+
if (arrayType.isArray) {
226+
return getChildArrayErrors(arrayType.children, v);
227+
} else if (arrayType.isObject) {
228+
return getChildErrors(arrayType.children, v);
231229
}
232-
});
230+
}
233231
});
234232
};
235233
const value = getValue();

lib/validator/validator.test.js

Lines changed: 131 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('Test for body params', () => {
5757
expect(resp.send).toHaveBeenCalledWith({
5858
error: [
5959
{
60-
location: 'body.page',
60+
location: 'body',
6161
message: 'Invalid Field Error',
6262
param: 'sorted',
6363
}
@@ -236,7 +236,7 @@ describe('Test for body params', () => {
236236
expect(req.locals.data).toEqual({
237237
error: [
238238
{
239-
location: 'body.page',
239+
location: 'body',
240240
message: 'Invalid Field Error',
241241
param: 'sorted',
242242
}
@@ -267,7 +267,7 @@ describe('Test for body params', () => {
267267
expect(resp.send).toHaveBeenCalledWith({
268268
error: [
269269
{
270-
location: 'body.page',
270+
location: 'body',
271271
message: 'Invalid Field Error',
272272
param: 'sorted',
273273
}
@@ -299,7 +299,7 @@ describe('Test for body params', () => {
299299
expect(resp.send).toHaveBeenCalledWith({
300300
error: [
301301
{
302-
location: 'body.page',
302+
location: 'body',
303303
message: 'Invalid Field Error',
304304
param: 'sorted',
305305
}
@@ -330,7 +330,7 @@ describe('Test for body params', () => {
330330
expect(resp.send).toHaveBeenCalledWith({
331331
error: [
332332
{
333-
location: 'body.page',
333+
location: 'body',
334334
message: 'Mandatory field sorted missing',
335335
param: 'sorted',
336336
}
@@ -361,7 +361,7 @@ describe('Test for body params', () => {
361361
expect(resp.send).toHaveBeenCalledWith({
362362
error: [
363363
{
364-
location: 'body.page',
364+
location: 'body',
365365
message: 'Invalid Field Error',
366366
param: 'sorted',
367367
}
@@ -434,7 +434,7 @@ describe('Test for body params', () => {
434434
expect(resp.send).toHaveBeenCalledWith({
435435
error: [
436436
{
437-
location: 'body.page',
437+
location: 'body',
438438
message: 'Invalid Field Error',
439439
param: 'sorted',
440440
}
@@ -488,7 +488,7 @@ describe('Test for body params', () => {
488488
expect(resp.send).toHaveBeenCalledWith({
489489
error: [
490490
{
491-
location: 'body.page',
491+
location: 'body',
492492
message: 'Invalid Field Error',
493493
param: 'sorted',
494494
}
@@ -542,7 +542,7 @@ describe('Test for body params', () => {
542542
expect(resp.send).toHaveBeenCalledWith({
543543
error: [
544544
{
545-
location: 'body.page',
545+
location: 'body',
546546
message: 'Invalid Field Error',
547547
param: 'sorted',
548548
}
@@ -711,7 +711,7 @@ describe('Test for body params', () => {
711711
expect(resp.send).toHaveBeenCalledWith({
712712
error: [
713713
{
714-
location: 'body.page',
714+
location: 'body',
715715
message: 'Invalid Field Error',
716716
param: 'sorted',
717717
}
@@ -742,7 +742,7 @@ describe('Test for body params', () => {
742742
expect(resp.send).toHaveBeenCalledWith({
743743
error: [
744744
{
745-
location: 'body.page',
745+
location: 'body',
746746
message: 'Invalid Field Error',
747747
param: 'sorted',
748748
}
@@ -773,7 +773,7 @@ describe('Test for body params', () => {
773773
expect(resp.send).toHaveBeenCalledWith({
774774
error: [
775775
{
776-
location: 'body.page',
776+
location: 'body',
777777
message: 'Invalid Field Error',
778778
param: 'sorted',
779779
}
@@ -850,7 +850,7 @@ describe('Test for body params', () => {
850850
expect(resp.send).toHaveBeenCalledWith({
851851
error: [
852852
{
853-
location: 'body.page',
853+
location: 'body',
854854
message: 'Invalid Field Error',
855855
param: 'sorted',
856856
}
@@ -881,7 +881,7 @@ describe('Test for body params', () => {
881881
expect(resp.send).toHaveBeenCalledWith({
882882
error: [
883883
{
884-
location: 'body.page',
884+
location: 'body',
885885
message: 'Invalid Field Error',
886886
param: 'sorted',
887887
}
@@ -912,7 +912,7 @@ describe('Test for body params', () => {
912912
expect(resp.send).toHaveBeenCalledWith({
913913
error: [
914914
{
915-
location: 'body.page',
915+
location: 'body',
916916
message: 'Invalid Field Error',
917917
param: 'sorted',
918918
}
@@ -943,7 +943,7 @@ describe('Test for body params', () => {
943943
expect(resp.send).toHaveBeenCalledWith({
944944
error: [
945945
{
946-
location: 'body.page',
946+
location: 'body',
947947
message: 'Invalid Field Error',
948948
param: 'sorted',
949949
}
@@ -997,7 +997,7 @@ describe('Test for body params', () => {
997997
expect(resp.send).toHaveBeenCalledWith({
998998
error: [
999999
{
1000-
location: 'body.page',
1000+
location: 'body',
10011001
message: 'Invalid Field Error',
10021002
param: 'sorted',
10031003
}
@@ -1051,7 +1051,7 @@ describe('Test for body params', () => {
10511051
expect(resp.send).toHaveBeenCalledWith({
10521052
error: [
10531053
{
1054-
location: 'body.page',
1054+
location: 'body',
10551055
message: 'Invalid Field Error',
10561056
param: 'sorted',
10571057
}
@@ -1105,11 +1105,123 @@ describe('Test for body params', () => {
11051105
expect(resp.send).toHaveBeenCalledWith({
11061106
error: [
11071107
{
1108-
location: 'body.page',
1108+
location: 'body',
11091109
message: 'Invalid Field Error',
11101110
param: 'sorted',
11111111
}
11121112
]
11131113
});
11141114
});
1115+
test('Test nested objects Failure case - Array test', () => {
1116+
const req = {
1117+
body: {
1118+
page: [
1119+
1, 2, 3, 'sdsd'
1120+
]
1121+
}
1122+
};
1123+
const validation = [
1124+
{param : 'page', location : 'body', isArray : true, children : [
1125+
{param: 'page-array', isRequired : true, isNumber: true },
1126+
]}
1127+
];
1128+
const response = {
1129+
mode: 'reject'
1130+
};
1131+
const validatorfn = validaor('body', validation, response);
1132+
validatorfn(req, resp, next);
1133+
expect(next).toHaveBeenCalledTimes(0);
1134+
expect(resp.status).toHaveBeenCalledTimes(1);
1135+
expect(resp.send).toHaveBeenCalledWith({
1136+
error: [
1137+
{
1138+
location: 'body',
1139+
message: 'Invalid Field Error',
1140+
param: 'page-array',
1141+
}
1142+
]
1143+
});
1144+
});
1145+
test('Test nested objects Success case - Array test', () => {
1146+
const req = {
1147+
body: {
1148+
page: [
1149+
1, 2, 3
1150+
]
1151+
}
1152+
};
1153+
const validation = [
1154+
{param : 'page', location : 'body', isArray : true, children : [
1155+
{param: 'page-array', isRequired : true, isNumber: true },
1156+
]}
1157+
];
1158+
const response = {
1159+
mode: 'reject'
1160+
};
1161+
const validatorfn = validaor('body', validation, response);
1162+
validatorfn(req, resp, next);
1163+
expect(next).toHaveBeenCalledTimes(1);
1164+
expect(resp.status).toHaveBeenCalledTimes(0);
1165+
});
1166+
test('Test nested objects Failure case - Array test 2', () => {
1167+
const req = {
1168+
body: {
1169+
page: [
1170+
{test: 1, test2: 2},
1171+
{test: 1, test2: 2},
1172+
{test: 1, test2: 'test'}
1173+
]
1174+
}
1175+
};
1176+
const validation = [
1177+
{param : 'page', location : 'body', isArray : true, children : [
1178+
{param: 'page-array', isRequired : true, isObject: true, children: [
1179+
{param: 'test', isNumber: true},
1180+
{param: 'test2', isNumber: true},
1181+
]},
1182+
]}
1183+
];
1184+
const response = {
1185+
mode: 'reject'
1186+
};
1187+
const validatorfn = validaor('body', validation, response);
1188+
validatorfn(req, resp, next);
1189+
expect(next).toHaveBeenCalledTimes(0);
1190+
expect(resp.status).toHaveBeenCalledTimes(1);
1191+
expect(resp.send).toHaveBeenCalledWith({
1192+
error: [
1193+
{
1194+
location: 'body',
1195+
message: 'Invalid Field Error',
1196+
param: 'test2',
1197+
}
1198+
]
1199+
});
1200+
});
1201+
test('Test nested objects Success case - Array test 2', () => {
1202+
const req = {
1203+
body: {
1204+
page: [
1205+
{test: 1, test2: 2},
1206+
{test: 1, test2: 2},
1207+
{test: 1, test2: 323}
1208+
]
1209+
}
1210+
};
1211+
const validation = [
1212+
{param : 'page', location : 'body', isArray : true, children : [
1213+
{param: 'page-array', isRequired : true, isObject: true, children: [
1214+
{param: 'test', isNumber: true},
1215+
{param: 'test2', isNumber: true},
1216+
]},
1217+
]}
1218+
];
1219+
const response = {
1220+
mode: 'reject'
1221+
};
1222+
const validatorfn = validaor('body', validation, response);
1223+
validatorfn(req, resp, next);
1224+
expect(next).toHaveBeenCalledTimes(1);
1225+
expect(resp.status).toHaveBeenCalledTimes(0);
1226+
});
11151227
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "expressjs-field-validator",
3-
"version": "3.0.0",
3+
"version": "3.0.2",
44
"description": "Plugin for validating field values of json request in expressjs",
55
"homepage": "https://gsmithun4.github.io/expressjs-field-validator",
66
"repository": {

0 commit comments

Comments
 (0)