Skip to content

Commit e403502

Browse files
committed
Fix
1 parent acd8890 commit e403502

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

lib/queryBuilder/queryBuilder.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const validateOps = () => {
55
validationObject.isRequired = true;
66
};
77
const addChild = (validationObject, child) => {
8+
validationObject.children = validationObject.children || [];
89
validationObject.children.push(child);
910
};
1011
const addChildren = (validationObject, children) => {
11-
validationObject.children.concat(children);
12+
validationObject.children = validationObject.children || [];
13+
validationObject.children.push(...children);
1214
};
1315
const isArray = (validationObject) => {
1416
validationObject.isArray = true;
@@ -36,7 +38,9 @@ const validateOps = () => {
3638
};
3739
const setNumberOptions = (validationObject, options = {}) => {
3840
validationObject.isNumber = true;
39-
validationObject.range = validationObject.range || {};
41+
if (options.min || options.max) {
42+
validationObject.range = validationObject.range || {};
43+
}
4044
if (options.min) {
4145
validationObject.range.min = options.min;
4246
}
@@ -80,7 +84,6 @@ const validateOps = () => {
8084
const setDebug = (validationObject, debug) => {
8185
validationObject.debug = debug;
8286
};
83-
const done = (validationObject) => validationObject;
8487

8588
return {
8689
isRequired,
@@ -94,7 +97,6 @@ const validateOps = () => {
9497
setDateOptions,
9598
setNumberOptions,
9699
sendErrorMessage,
97-
done,
98100
setMode,
99101
setErrorCode,
100102
setDebug,
@@ -133,7 +135,10 @@ const setParam = (param, location, responseObject) => {
133135
return fieldProperties(location, responseObject).param(param);
134136
};
135137
const fieldProperties = (location, responseObj) => {
136-
const validationObject = { location, children: [] };
138+
const validationObject = {};
139+
if (location) {
140+
validationObject.location = location;
141+
}
137142
const responseObject = responseObj;
138143

139144
const param = (par) => {validationObject.param = par; return fieldFunctions;};
@@ -195,4 +200,5 @@ module.exports = {
195200
validateBody,
196201
validateParam,
197202
validateQuery,
203+
param: setParam,
198204
};

lib/queryBuilder/queryBuilder.test.js

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { validateBody, validateParam, validateQuery } = require('./queryBuilder');
1+
const { validateBody, validateParam, validateQuery, param } = require('./queryBuilder');
22
const validator = require('../validator');
33
jest.mock('../validator');
44

@@ -10,55 +10,46 @@ describe('Test for validateBody', () => {
1010
validateBody().isToBeRejected().sendErrorCode(500).debug(true).param('test').isNumber().done();
1111
expect(validator).toBeCalledWith(
1212
{
13-
children: [],
1413
param: 'test',
1514
location: 'body',
1615
isNumber: true,
17-
range: {}
1816
}, { mode: 'reject', errorCode: 500, debug: true }
1917
);
2018
});
2119
test('validateBody response object', () => {
2220
validateBody().isToBeForwarded().sendErrorCode(500).debug(false).param('test').isNumber().done();
2321
expect(validator).toBeCalledWith(
2422
{
25-
children: [],
2623
param: 'test',
2724
location: 'body',
2825
isNumber: true,
29-
range: {}
3026
}, { mode: 'forward', errorCode: 500, debug: false }
3127
);
3228
});
3329
test('validateBody response object empty', () => {
3430
validateBody().param('test').isNumber().done();
3531
expect(validator).toBeCalledWith(
3632
{
37-
children: [],
3833
param: 'test',
3934
location: 'body',
4035
isNumber: true,
41-
range: {}
4236
}, {}
4337
);
4438
});
4539
test('validateBody validator object isNumber', () => {
4640
validateBody().param('test').isNumber().done();
4741
expect(validator).toBeCalledWith(
4842
{
49-
children: [],
5043
param: 'test',
5144
location: 'body',
5245
isNumber: true,
53-
range: {}
5446
}, {}
5547
);
5648
});
5749
test('validateBody validator object isNumber and min max ranges', () => {
5850
validateBody().param('test').minimumNumber(1).maximumNumber(10).done();
5951
expect(validator).toBeCalledWith(
6052
{
61-
children: [],
6253
param: 'test',
6354
location: 'body',
6455
isNumber: true,
@@ -70,7 +61,6 @@ describe('Test for validateBody', () => {
7061
validateBody().param('test').isBoolean().done();
7162
expect(validator).toBeCalledWith(
7263
{
73-
children: [],
7464
param: 'test',
7565
location: 'body',
7666
isBoolean: true,
@@ -81,7 +71,6 @@ describe('Test for validateBody', () => {
8171
validateBody().param('test').isRequired().done();
8272
expect(validator).toBeCalledWith(
8373
{
84-
children: [],
8574
param: 'test',
8675
location: 'body',
8776
isRequired: true,
@@ -92,18 +81,72 @@ describe('Test for validateBody', () => {
9281
validateBody().param('test').isArray().done();
9382
expect(validator).toBeCalledWith(
9483
{
95-
children: [],
9684
param: 'test',
9785
location: 'body',
9886
isArray: true,
9987
}, {}
10088
);
10189
});
90+
test('validateBody validator object isArray and object child', () => {
91+
validateBody().param('test').isArray()
92+
.addChildren([
93+
param('child1').isObject().addChild(
94+
param('child11').isNumber().end()
95+
).end(),
96+
param('child2').isObject().addChild(
97+
param('child21').isNumber().end()
98+
).end()
99+
]).done();
100+
expect(validator).toBeCalledWith(
101+
{
102+
param: 'test',
103+
location: 'body',
104+
isArray: true,
105+
children: [
106+
{ param: 'child1', isObject: true, children: [
107+
{ param: 'child11', isNumber: true }
108+
] },
109+
{ param: 'child2', isObject: true, children: [
110+
{ param: 'child21', isNumber: true }
111+
] }
112+
]
113+
}, {}
114+
);
115+
});
116+
test('validateBody validator object isArray and object child nested', () => {
117+
validateBody().param('test').isArray()
118+
.addChildren([
119+
param('child1').isObject().addChild(
120+
param('child11').isObject().addChild(
121+
param('child111').isNumber().end()
122+
).end()
123+
).end(),
124+
param('child2').isObject().addChild(
125+
param('child21').isNumber().end()
126+
).end()
127+
]).done();
128+
expect(validator).toBeCalledWith(
129+
{
130+
param: 'test',
131+
location: 'body',
132+
isArray: true,
133+
children: [
134+
{ param: 'child1', isObject: true, children: [
135+
{ param: 'child11', isObject: true, children: [
136+
{ param: 'child111', isNumber: true }
137+
] }
138+
] },
139+
{ param: 'child2', isObject: true, children: [
140+
{ param: 'child21', isNumber: true }
141+
] }
142+
]
143+
}, {}
144+
);
145+
});
102146
test('validateBody validator object shouldInclude', () => {
103147
validateBody().param('test').shouldInclude(['a','b']).done();
104148
expect(validator).toBeCalledWith(
105149
{
106-
children: [],
107150
param: 'test',
108151
location: 'body',
109152
includes: ['a','b'],
@@ -114,7 +157,6 @@ describe('Test for validateBody', () => {
114157
validateBody().param('test').shouldExclude(['a','b']).done();
115158
expect(validator).toBeCalledWith(
116159
{
117-
children: [],
118160
param: 'test',
119161
location: 'body',
120162
excludes: ['a','b'],
@@ -125,7 +167,6 @@ describe('Test for validateBody', () => {
125167
validateBody().param('test').isObject().done();
126168
expect(validator).toBeCalledWith(
127169
{
128-
children: [],
129170
param: 'test',
130171
location: 'body',
131172
isObject: true,
@@ -136,7 +177,6 @@ describe('Test for validateBody', () => {
136177
validateBody().param('test').isEmail().done();
137178
expect(validator).toBeCalledWith(
138179
{
139-
children: [],
140180
param: 'test',
141181
location: 'body',
142182
isEmail: true,
@@ -147,7 +187,6 @@ describe('Test for validateBody', () => {
147187
validateBody().param('test').isDate().done();
148188
expect(validator).toBeCalledWith(
149189
{
150-
children: [],
151190
param: 'test',
152191
location: 'body',
153192
isDate: true,
@@ -158,7 +197,6 @@ describe('Test for validateBody', () => {
158197
validateBody().param('test').dateFormat('MM-DD-YYYY').done();
159198
expect(validator).toBeCalledWith(
160199
{
161-
children: [],
162200
param: 'test',
163201
location: 'body',
164202
isDate: true,
@@ -170,7 +208,6 @@ describe('Test for validateBody', () => {
170208
validateBody().param('test').minimumLength(2).maximumLength(5).done();
171209
expect(validator).toBeCalledWith(
172210
{
173-
children: [],
174211
param: 'test',
175212
location: 'body',
176213
length: { min: 2, max: 5 }
@@ -181,7 +218,6 @@ describe('Test for validateBody', () => {
181218
validateBody().param('test').isMobileNumberWithCountryCode('91').done();
182219
expect(validator).toBeCalledWith(
183220
{
184-
children: [],
185221
param: 'test',
186222
location: 'body',
187223
mobileNumber: {
@@ -195,7 +231,6 @@ describe('Test for validateBody', () => {
195231
validateBody().param('test').isMobileNumberWithCountryCode('91').isMobileNumberWithCountryCodeMandatory().done();
196232
expect(validator).toBeCalledWith(
197233
{
198-
children: [],
199234
param: 'test',
200235
location: 'body',
201236
mobileNumber: {
@@ -210,7 +245,6 @@ describe('Test for validateBody', () => {
210245
validateBody().param('test').isMobileNumberWithMaximumLength(10).done();
211246
expect(validator).toBeCalledWith(
212247
{
213-
children: [],
214248
param: 'test',
215249
location: 'body',
216250
mobileNumber: {
@@ -223,7 +257,6 @@ describe('Test for validateBody', () => {
223257
validateBody().param('test').isMobileNumberWithMinimumLength(1).done();
224258
expect(validator).toBeCalledWith(
225259
{
226-
children: [],
227260
param: 'test',
228261
location: 'body',
229262
mobileNumber: {
@@ -236,7 +269,6 @@ describe('Test for validateBody', () => {
236269
validateBody().param('test').isMobileNumberWithMinimumLength(1).isMobileNumberWithMaximumLength(10).done();
237270
expect(validator).toBeCalledWith(
238271
{
239-
children: [],
240272
param: 'test',
241273
location: 'body',
242274
mobileNumber: {
@@ -249,7 +281,6 @@ describe('Test for validateBody', () => {
249281
validateBody().param('test').sendErrorMessage('Invalid Data').done();
250282
expect(validator).toBeCalledWith(
251283
{
252-
children: [],
253284
param: 'test',
254285
location: 'body',
255286
message: 'Invalid Data',
@@ -265,11 +296,9 @@ describe('Test for validateParam', () => {
265296
validateParam().isToBeForwarded().sendErrorCode(500).debug(false).param('test').isNumber().done();
266297
expect(validator).toBeCalledWith(
267298
{
268-
children: [],
269299
param: 'test',
270300
location: 'param',
271301
isNumber: true,
272-
range: {}
273302
}, { mode: 'forward', errorCode: 500, debug: false }
274303
);
275304
});
@@ -282,11 +311,9 @@ describe('Test for validateQuery', () => {
282311
validateQuery().isToBeForwarded().sendErrorCode(500).debug(false).param('test').isNumber().done();
283312
expect(validator).toBeCalledWith(
284313
{
285-
children: [],
286314
param: 'test',
287315
location: 'query',
288316
isNumber: true,
289-
range: {}
290317
}, { mode: 'forward', errorCode: 500, debug: false }
291318
);
292319
});

0 commit comments

Comments
 (0)