Skip to content

Commit 0ad9181

Browse files
authored
Merge pull request #13 from gsmithun4/development
Added custom validator function
2 parents 130b77c + f67b5aa commit 0ad9181

File tree

7 files changed

+1661
-1438
lines changed

7 files changed

+1661
-1438
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
/lib/checkService/checkService.test.js
99
/lib/skipService/skipService.js
1010
/lib/skipService/skipService.test.js
11+
/lib/queryBuilder/queryBuilder.test.js
12+
/lib/queryBuilder/queryBuilder.test.js
1113
_config.yml
1214
.eslintrc.js
1315
sonar-project.properties

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ Expects a date with default format `YYYY-MM-DD`
123123
specify date format, supported
124124
```
125125
YYYY-MM-DD
126-
DD-MM-YYYY'
127-
MM-DD-YYYY'
128-
YYYY/MM/DD'
129-
DD/MM/YYYY'
130-
MM/DD/YYYY'
126+
DD-MM-YYYY
127+
MM-DD-YYYY
128+
YYYY/MM/DD
129+
DD/MM/YYYY
130+
MM/DD/YYYY
131131
```
132132
##### minimumNumber(min)
133133
* `min` *Mandatory* Number
@@ -151,13 +151,26 @@ Expects number/string and must not be one of given array `excludes`
151151
* `countryCode` *Mandatory* String
152152
Expects mobile number with or without `countryCode`
153153
##### isMobileNumberWithCountryCodeMandatory()
154-
Expects mobile number which should starts with country code set at `isMobileNumberWithCountryCode`
154+
Expects mobile number which should starts with the country code set with `isMobileNumberWithCountryCode`
155155
##### isMobileNumberWithMinimumLength(min)
156156
* `min` *Mandatory* Number
157157
Minimum length of mobile number without country code
158158
##### isMobileNumberWithMaximumLength(max)
159159
* `max` *Mandatory* Number
160160
Maximum length of mobile number without country code
161+
##### customValidator(function)
162+
* `function` *Mandatory* Function
163+
A function with arguments (`value`, `req`, `error`)
164+
`value` is the value of the field
165+
`req` request object
166+
`error` function with takes error message, should be called on error
167+
```js
168+
(value, req, error) => {
169+
if (value !== 100) {
170+
error('Invalid value customValidator');
171+
}
172+
}
173+
```
161174
##### addChild(child)
162175
* `child` *Mandatory* field definition object
163176
Add a child object for arrays and objects
@@ -292,6 +305,13 @@ Error object
292305
```
293306
##### addParams(paramList)
294307
* `paramList` *Mandatory* Array of field definition objects
308+
```js
309+
validateBody().addParams([
310+
// Add List of definition here
311+
param('field1').isRequired().end(),
312+
]).done()
313+
```
314+
Definintion of a field here : [Defining a Field](#defining-a-field)
295315
##### done() :bangbang::bangbang: Mandatory
296316
Ends a validation definition
297317
## Dealing with nested objects
@@ -317,7 +337,7 @@ Should send http status code 500 in case of error
317337
```js
318338
router.post('/users/:id',
319339
validateBody().isToBeRejected().sendErrorCode(500).addParams([
320-
param('field1').isRequired().end(),
340+
param('field1').isRequired().end(), // Use end() to end a definition
321341
param('field2').isRequired().isArray().isRequired().addChild(
322342
param('field2-array').isObject().addChild( // field2-array is for tracking, you can give any name here
323343
param('field21').isNumber().isRequired().end()
@@ -330,7 +350,12 @@ validateBody().isToBeRejected().sendErrorCode(500).addParams([
330350
param('field4').isRequired().isArray().isRequired().addChild(
331351
param('field4-array').isNumber().end()
332352
).end(),
333-
]).done(),
353+
]).done(), // Use done() to end a validation
354+
validateParam().isToBeRejected().sendErrorCode(500).addParams([
355+
param('field1').isRequired().end(), // Use end() to end a definition
356+
]).done(), // Use done() to end a validation
357+
// define validateQuery(),
358+
// define validateHeader(),
334359
(req, res, next) => {
335360

336361
// Main Service Here

lib/queryBuilder/queryBuilder.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ const validateOps = () => {
8686
const setDebug = (validationObject, debug) => {
8787
validationObject.debug = debug;
8888
};
89+
90+
const setValidateFunction = (validationObject, func) => {
91+
validationObject.customValidator = func;
92+
};
8993

9094
return {
9195
isRequired,
@@ -104,6 +108,7 @@ const validateOps = () => {
104108
setDebug,
105109
setMobileNumberOptions,
106110
setLengthOptions,
111+
setValidateFunction,
107112
};
108113
};
109114
const operations = validateOps();
@@ -173,7 +178,8 @@ const fieldProperties = (location, responseObj) => {
173178
const isMobileNumberWithCountryCodeMandatory = () => {operations.setMobileNumberOptions(validationObject, { isCountryCodeMandatory: true }); return fieldFunctions;};
174179
const isMobileNumberWithMinimumLength = (min) => {operations.setMobileNumberOptions(validationObject, { length: { min } }); return fieldFunctions;};
175180
const isMobileNumberWithMaximumLength = (max) => {operations.setMobileNumberOptions(validationObject, { length: { max } }); return fieldFunctions;};
176-
181+
const customValidator = (func) => {operations.setValidateFunction(validationObject, func); return fieldFunctions;};
182+
177183
const fieldFunctions = {
178184
done,
179185
isRequired,
@@ -198,6 +204,7 @@ const fieldProperties = (location, responseObj) => {
198204
isMobileNumberWithCountryCodeMandatory,
199205
isMobileNumberWithMinimumLength,
200206
isMobileNumberWithMaximumLength,
207+
customValidator,
201208
};
202209
return {
203210
addChildren,

lib/queryBuilder/queryBuilder.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ describe('Test for validateBody', () => {
184184
}], {}
185185
);
186186
});
187+
test('validateBody validator object customValidator', () => {
188+
const validatorfn = jest.fn();
189+
validateBody().addParams([
190+
param('test').customValidator(validatorfn).end()
191+
]).done();
192+
expect(validator).toBeCalledWith('body',
193+
[{
194+
param: 'test',
195+
customValidator: validatorfn,
196+
}], {}
197+
);
198+
});
187199
test('validateBody validator object isEmail', () => {
188200
validateBody().addParams([
189201
param('test').isEmail().end()

lib/validator/validator.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = (location = 'body', validation = [], response = {}) => {
4141
const checkFormat = (value, validateObj) => {
4242
const { param, isRequired, isNumber, isEmail, isBoolean,
4343
length, message, isDate, format, isArray, isObject, range,
44-
includes, excludes, mobileNumber } = validateObj;
44+
includes, excludes, mobileNumber, customValidator } = validateObj;
4545

4646
const testRegEx = (regex, value) => {
4747
return regex.test(value);
@@ -114,8 +114,17 @@ module.exports = (location = 'body', validation = [], response = {}) => {
114114
}
115115
return true;
116116
};
117+
const throwError = (errMsg) => {
118+
throw new Error(errMsg || 'customValidator error');
119+
};
117120

118121
try {
122+
if (customValidator) {
123+
if (typeof customValidator !== 'function') {
124+
throw new Error('Configured customValidator is not a function');
125+
}
126+
customValidator(value, req, throwError);
127+
}
119128
const isEmpty = checkEmpty(value);
120129
if (isRequired) {
121130
if (isEmpty) {

0 commit comments

Comments
 (0)