Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/lib/checkService/checkService.test.js
/lib/skipService/skipService.js
/lib/skipService/skipService.test.js
/lib/queryBuilder/queryBuilder.test.js
/lib/queryBuilder/queryBuilder.test.js
_config.yml
.eslintrc.js
sonar-project.properties
Expand Down
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ Expects a date with default format `YYYY-MM-DD`
specify date format, supported
```
YYYY-MM-DD
DD-MM-YYYY'
MM-DD-YYYY'
YYYY/MM/DD'
DD/MM/YYYY'
MM/DD/YYYY'
DD-MM-YYYY
MM-DD-YYYY
YYYY/MM/DD
DD/MM/YYYY
MM/DD/YYYY
```
##### minimumNumber(min)
* `min` *Mandatory* Number
Expand All @@ -151,13 +151,26 @@ Expects number/string and must not be one of given array `excludes`
* `countryCode` *Mandatory* String
Expects mobile number with or without `countryCode`
##### isMobileNumberWithCountryCodeMandatory()
Expects mobile number which should starts with country code set at `isMobileNumberWithCountryCode`
Expects mobile number which should starts with the country code set with `isMobileNumberWithCountryCode`
##### isMobileNumberWithMinimumLength(min)
* `min` *Mandatory* Number
Minimum length of mobile number without country code
##### isMobileNumberWithMaximumLength(max)
* `max` *Mandatory* Number
Maximum length of mobile number without country code
##### customValidator(function)
* `function` *Mandatory* Function
A function with arguments (`value`, `req`, `error`)
`value` is the value of the field
`req` request object
`error` function with takes error message, should be called on error
```js
(value, req, error) => {
if (value !== 100) {
error('Invalid value customValidator');
}
}
```
##### addChild(child)
* `child` *Mandatory* field definition object
Add a child object for arrays and objects
Expand Down Expand Up @@ -292,6 +305,13 @@ Error object
```
##### addParams(paramList)
* `paramList` *Mandatory* Array of field definition objects
```js
validateBody().addParams([
// Add List of definition here
param('field1').isRequired().end(),
]).done()
```
Definintion of a field here : [Defining a Field](#defining-a-field)
##### done() :bangbang::bangbang: Mandatory
Ends a validation definition
## Dealing with nested objects
Expand All @@ -317,7 +337,7 @@ Should send http status code 500 in case of error
```js
router.post('/users/:id',
validateBody().isToBeRejected().sendErrorCode(500).addParams([
param('field1').isRequired().end(),
param('field1').isRequired().end(), // Use end() to end a definition
param('field2').isRequired().isArray().isRequired().addChild(
param('field2-array').isObject().addChild( // field2-array is for tracking, you can give any name here
param('field21').isNumber().isRequired().end()
Expand All @@ -330,7 +350,12 @@ validateBody().isToBeRejected().sendErrorCode(500).addParams([
param('field4').isRequired().isArray().isRequired().addChild(
param('field4-array').isNumber().end()
).end(),
]).done(),
]).done(), // Use done() to end a validation
validateParam().isToBeRejected().sendErrorCode(500).addParams([
param('field1').isRequired().end(), // Use end() to end a definition
]).done(), // Use done() to end a validation
// define validateQuery(),
// define validateHeader(),
(req, res, next) => {

// Main Service Here
Expand Down
9 changes: 8 additions & 1 deletion lib/queryBuilder/queryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ const validateOps = () => {
const setDebug = (validationObject, debug) => {
validationObject.debug = debug;
};

const setValidateFunction = (validationObject, func) => {
validationObject.customValidator = func;
};

return {
isRequired,
Expand All @@ -104,6 +108,7 @@ const validateOps = () => {
setDebug,
setMobileNumberOptions,
setLengthOptions,
setValidateFunction,
};
};
const operations = validateOps();
Expand Down Expand Up @@ -173,7 +178,8 @@ const fieldProperties = (location, responseObj) => {
const isMobileNumberWithCountryCodeMandatory = () => {operations.setMobileNumberOptions(validationObject, { isCountryCodeMandatory: true }); return fieldFunctions;};
const isMobileNumberWithMinimumLength = (min) => {operations.setMobileNumberOptions(validationObject, { length: { min } }); return fieldFunctions;};
const isMobileNumberWithMaximumLength = (max) => {operations.setMobileNumberOptions(validationObject, { length: { max } }); return fieldFunctions;};

const customValidator = (func) => {operations.setValidateFunction(validationObject, func); return fieldFunctions;};

const fieldFunctions = {
done,
isRequired,
Expand All @@ -198,6 +204,7 @@ const fieldProperties = (location, responseObj) => {
isMobileNumberWithCountryCodeMandatory,
isMobileNumberWithMinimumLength,
isMobileNumberWithMaximumLength,
customValidator,
};
return {
addChildren,
Expand Down
12 changes: 12 additions & 0 deletions lib/queryBuilder/queryBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ describe('Test for validateBody', () => {
}], {}
);
});
test('validateBody validator object customValidator', () => {
const validatorfn = jest.fn();
validateBody().addParams([
param('test').customValidator(validatorfn).end()
]).done();
expect(validator).toBeCalledWith('body',
[{
param: 'test',
customValidator: validatorfn,
}], {}
);
});
test('validateBody validator object isEmail', () => {
validateBody().addParams([
param('test').isEmail().end()
Expand Down
11 changes: 10 additions & 1 deletion lib/validator/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = (location = 'body', validation = [], response = {}) => {
const checkFormat = (value, validateObj) => {
const { param, isRequired, isNumber, isEmail, isBoolean,
length, message, isDate, format, isArray, isObject, range,
includes, excludes, mobileNumber } = validateObj;
includes, excludes, mobileNumber, customValidator } = validateObj;

const testRegEx = (regex, value) => {
return regex.test(value);
Expand Down Expand Up @@ -114,8 +114,17 @@ module.exports = (location = 'body', validation = [], response = {}) => {
}
return true;
};
const throwError = (errMsg) => {
throw new Error(errMsg || 'customValidator error');
};

try {
if (customValidator) {
if (typeof customValidator !== 'function') {
throw new Error('Configured customValidator is not a function');
}
customValidator(value, req, throwError);
}
const isEmpty = checkEmpty(value);
if (isRequired) {
if (isEmpty) {
Expand Down
Loading