diff --git a/.npmignore b/.npmignore index eca25a8..0fe4c40 100644 --- a/.npmignore +++ b/.npmignore @@ -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 diff --git a/README.md b/README.md index f0a0266..c30b968 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 diff --git a/lib/queryBuilder/queryBuilder.js b/lib/queryBuilder/queryBuilder.js index f46a227..47219bc 100644 --- a/lib/queryBuilder/queryBuilder.js +++ b/lib/queryBuilder/queryBuilder.js @@ -86,6 +86,10 @@ const validateOps = () => { const setDebug = (validationObject, debug) => { validationObject.debug = debug; }; + + const setValidateFunction = (validationObject, func) => { + validationObject.customValidator = func; + }; return { isRequired, @@ -104,6 +108,7 @@ const validateOps = () => { setDebug, setMobileNumberOptions, setLengthOptions, + setValidateFunction, }; }; const operations = validateOps(); @@ -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, @@ -198,6 +204,7 @@ const fieldProperties = (location, responseObj) => { isMobileNumberWithCountryCodeMandatory, isMobileNumberWithMinimumLength, isMobileNumberWithMaximumLength, + customValidator, }; return { addChildren, diff --git a/lib/queryBuilder/queryBuilder.test.js b/lib/queryBuilder/queryBuilder.test.js index a25b201..88698c3 100644 --- a/lib/queryBuilder/queryBuilder.test.js +++ b/lib/queryBuilder/queryBuilder.test.js @@ -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() diff --git a/lib/validator/validator.js b/lib/validator/validator.js index 55b7b37..2ea01ec 100644 --- a/lib/validator/validator.js +++ b/lib/validator/validator.js @@ -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); @@ -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) { diff --git a/lib/validator/validator.test.js b/lib/validator/validator.test.js index 077508b..d696a68 100644 --- a/lib/validator/validator.test.js +++ b/lib/validator/validator.test.js @@ -11,229 +11,233 @@ describe('Test for body params', () => { jest.clearAllMocks(); }); - test('Test nested objects Success case - Boolean test', () => { - const req = { - body: { - page: { - sorted: 'True' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Boolean test', () => { - const req = { - body: { - page: { - sorted: 'error-string' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + describe('Boolean', () => { + test('Test nested objects Success case - Boolean test', () => { + const req = { + body: { + page: { + sorted: 'True' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Success case - Boolean test - path params', () => { - const req = { - params: { - page: true - } - }; - const validation = [ - {param : 'page', location : 'params', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('params', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Boolean test - path params', () => { - const req = { - params: { - page: { - sorted: 'error-string' - } - } - }; - const validation = [ - {param : 'page', location : 'params', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('params', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'params', - message: 'Invalid Field Error', - param: 'page', + + test('Test nested objects Error case - Boolean test', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); - }); - - test('Test nested objects Error case - Required test - path params', () => { - const req = { - params: {} - }; - const validation = [ - {param : 'page', location : 'params', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('params', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'params', - message: 'Invalid Field Error', - param: 'page', - } - ] + + test('Test nested objects Success case - Boolean test - path params', () => { + const req = { + params: { + page: true + } + }; + const validation = [ + {param : 'page', location : 'params', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('params', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Success case - Boolean test - path query', () => { - const req = { - query: { - page: true - } - }; - const validation = [ - {param : 'page', location : 'query', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('query', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Boolean test - path query', () => { - const req = { - query: { - page: { - sorted: 'error-string' - } - } - }; - const validation = [ - {param : 'page', location : 'query', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('query', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'query', - message: 'Invalid Field Error', - param: 'page', + + test('Test nested objects Error case - Boolean test - path params', () => { + const req = { + params: { + page: { + sorted: 'error-string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'params', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('params', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'params', + message: 'Invalid Field Error', + param: 'page', + } + ] + }); }); - }); - - test('Test nested objects Success case - Boolean test - path locals', () => { - const req = { - locals: { - page: true - } - }; - const validation = [ - {param : 'page', location : 'locals', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('locals', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Boolean test - path locals', () => { - const req = { - locals: { - page: { - sorted: 'error-string' + + test('Test nested objects Error case - Required test - path params', () => { + const req = { + params: {} + }; + const validation = [ + {param : 'page', location : 'params', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('params', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'params', + message: 'Invalid Field Error', + param: 'page', + } + ] + }); + }); + + test('Test nested objects Success case - Boolean test - path query', () => { + const req = { + query: { + page: true + } + }; + const validation = [ + {param : 'page', location : 'query', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('query', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - Boolean test - path query', () => { + const req = { + query: { + page: { + sorted: 'error-string' + } } - } - }; - const validation = [ - {param : 'page', location : 'locals', isRequired : true, isBoolean : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('locals', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'locals', - message: 'Invalid Field Error', - param: 'page', + }; + const validation = [ + {param : 'page', location : 'query', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('query', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'query', + message: 'Invalid Field Error', + param: 'page', + } + ] + }); + }); + + test('Test nested objects Success case - Boolean test - path locals', () => { + const req = { + locals: { + page: true + } + }; + const validation = [ + {param : 'page', location : 'locals', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('locals', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - Boolean test - path locals', () => { + const req = { + locals: { + page: { + sorted: 'error-string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'locals', isRequired : true, isBoolean : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('locals', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'locals', + message: 'Invalid Field Error', + param: 'page', + } + ] + }); }); }); + + test('Test nested objects Error case - Mode test forward', () => { const req = { body: { @@ -269,1323 +273,1487 @@ describe('Test for body params', () => { }); }); - test('Test nested objects Error case - Status code test', () => { - const req = { - body: { - page: { - sorted: 'error-string' + describe('Error code/ status code test on failure', () => { + test('Test nested objects Error case - Status code test', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledWith(422); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledWith(422); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Error case - Predefined Status code test', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject', + errorCode: 400 + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledWith(400); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Error case - Predefined error message', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true, message:'Mandatory field sorted missing'}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Mandatory field sorted missing', + param: 'sorted', + } + ] + }); }); }); - test('Test nested objects Error case - Predefined Status code test', () => { - const req = { - body: { - page: { - sorted: 'error-string' + describe('isRequired test', () => { + test('Test nested objects Error case - Required Test', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject', - errorCode: 400 - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledWith(400); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Success case - Required Test 2', () => { + const req = { + body: { + page: {} } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isBoolean : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); }); }); - test('Test nested objects Error case - Predefined error message', () => { - const req = { - body: { - page: { - sorted: 'error-string' + describe('isNumber test', () => { + test('Test nested objects Success case - Number test', () => { + const req = { + body: { + page: { + sorted: '5' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isBoolean : true, message:'Mandatory field sorted missing'}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Mandatory field sorted missing', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - Number test', () => { + const req = { + body: { + page: { + sorted: 'error-string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); - }); - - test('Test nested objects Error case - Required Test', () => { - const req = { - body: { - page: { - sorted: 'error-string' + + test('Test nested objects Success case - Number test zero', () => { + const req = { + body: { + page: { + sorted: '0' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Success case - Number test zero number', () => { + const req = { + body: { + page: { + sorted: 0 + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Success case - Required Test 2', () => { - const req = { - body: { - page: {} - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isBoolean : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - }); - - test('Test nested objects Success case - Number test', () => { - const req = { - body: { - page: { - sorted: '5' + + test('Test nested objects Success case - Number test - Range', () => { + const req = { + body: { + page: { + sorted: 2 + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); - test('Test nested objects Error case - Number test', () => { - const req = { - body: { - page: { - sorted: 'error-string' + test('Test nested objects Error case - Number test - Range', () => { + const req = { + body: { + page: { + sorted: 10 + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Error case - Number test - Range min', () => { + const req = { + body: { + page: { + sorted: 1 + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); }); - - test('Test nested objects Success case - Number test - Range', () => { - const req = { - body: { - page: { - sorted: 2 + + describe('customValidator test', () => { + test('Test error case - customValidator not a function', () => { + const req = { + body: { + page: { + sorted: 100 + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Number test - Range', () => { - const req = { - body: { - page: { - sorted: 10 + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isNumber : true, customValidator: 'not-a-function'}, + ]} + ]; + const response = { + mode: 'reject', + debug: true, + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error :: 100 : Error: Configured customValidator is not a function', + param: 'sorted', + } + ] + }); + }); + + test('Test error case - customValidator calls error', () => { + const req = { + body: { + page: { + sorted: 101 + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isNumber : true, customValidator: (value, req, error) => { + if (value !== 100) { + error('Invalid value customValidator'); + } + }}, + ]} + ]; + const response = { + mode: 'reject', + debug: true, + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error :: 101 : Error: Invalid value customValidator', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Success case - customValidator', () => { + const req = { + body: { + page: { + sorted: 100 + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isNumber : true, customValidator: (value, req, error) => { + if (value !== 100) { + error('Invalid value customValidator'); + } + }}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); }); - - test('Test nested objects Error case - Number test - Range min', () => { - const req = { - body: { - page: { - sorted: 1 - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isNumber : true, range: { min: 2, max: 9 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + + describe('isObject test', () => { + test('Test nested objects Error case - Object test', () => { + const req = { + body: { + page: { + sorted: 'string' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isObject : true}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); - }); - test('Test nested objects Error case - Object test', () => { - const req = { - body: { - page: { - sorted: 'string' + test('Test nested objects Failure case - Object test', () => { + const req = { + body: { + pageHead: { + page: [ + {test: 1, test2: 2}, + {test: 1, test2: 2}, + {test: 1, test2: 'test'} + ] + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isObject : true}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'pageHead', location : 'body', isObject : true, children : [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]}, + ]} + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'test2', + } + ] + }); + }); + + test('Test nested objects Failure case - Object test 2', () => { + const req = { + body: { + pageHead: { + page: { + test: 1, test2: 'test' + } + } } - ] + }; + const validation = [ + {param : 'pageHead', location : 'body', isObject : true, children : [ + {param : 'page', location : 'body', isObject : true, children : [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]} + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'test2', + } + ] + }); }); }); - - test('Test nested objects Success case - Email test', () => { - const req = { - body: { - page: { - sorted: 'validemial@email.com' + + describe('Email test', () => { + test('Test nested objects Success case - Email test', () => { + const req = { + body: { + page: { + sorted: 'validemial@email.com' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Email test - default location must be body', () => { - const req = { - body: { - page: { - sorted: 'validemial@email.com' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor(null, validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Email test', () => { - const req = { - body: { - page: { - sorted: 'not-a-valid-email' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', - } - ] - }); - }); - - test('Test nested objects Success case - Date test Format defauly YYYY-MM-DD', () => { - const req = { - body: { - page: { - sorted: '2012-09-19' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Date test - Format DD-MM-YYYY', () => { - const req = { - body: { - page: { - sorted: '19-08-2019' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'DD-MM-YYYY' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Date test - Format MM-DD-YYYY', () => { - const req = { - body: { - page: { - sorted: '08-18-2019' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'MM-DD-YYYY' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Date test - Format MM/DD/YYYY', () => { - const req = { - body: { - page: { - sorted: '12/18/2019' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'MM/DD/YYYY' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Date test - Format DD/MM/YYYY', () => { - const req = { - body: { - page: { - sorted: '12/07/2019' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'DD/MM/YYYY' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Date test - Format YYYY/MM/DD', () => { - const req = { - body: { - page: { - sorted: '2901/07/19' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'YYYY/MM/DD' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Date test', () => { - const req = { - body: { - page: { - sorted: 'not-a-valid-date' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate : true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', - } - ] - }); - }); - - test('Test nested objects Error case - Date test - Format', () => { - const req = { - body: { - page: { - sorted: '09-08-3045' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY-MM-DD' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', - } - ] - }); - }); - - test('Test nested objects Error case - Date test - Invalid month/date', () => { - const req = { - body: { - page: { - sorted: '2222-89-45' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY-MM-DD' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', - } - ] - }); - }); - - test('Test nested objects Error case - Date test - Invalid Format', () => { - const req = { - body: { - page: { - sorted: '2019-23-02' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY/DD/MM' }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', - } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - - test('Test nested objects Success case - Mobile number test', () => { - const req = { - body: { - page: { - sorted: '919035803903' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Success case - Mobile number test - country code not mandatory', () => { - const req = { - body: { - page: { - sorted: '919035803903' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : false, length: {min : 1, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - Mobile number test - country code mandatory', () => { - const req = { - body: { - page: { - sorted: '9035803903' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + + test('Test nested objects Success case - Email test - default location must be body', () => { + const req = { + body: { + page: { + sorted: 'validemial@email.com' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor(null, validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Error case - Mobile number test - length range min', () => { - const req = { - body: { - page: { - sorted: '919035844' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 8, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + + test('Test nested objects Error case - Email test', () => { + const req = { + body: { + page: { + sorted: 'not-a-valid-email' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isEmail : true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); }); - test('Test nested objects Error case - Mobile number test - length range max', () => { - const req = { - body: { - page: { - sorted: '9190358444444' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 8, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + describe('Date test', () => { + test('Test nested objects Success case - Date test Format defauly YYYY-MM-DD', () => { + const req = { + body: { + page: { + sorted: '2012-09-19' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Error case - Mobile number test', () => { - const req = { - body: { - page: { - sorted: 'not-a-valid-mobile number' + + test('Test nested objects Success case - Date test - Format DD-MM-YYYY', () => { + const req = { + body: { + page: { + sorted: '19-08-2019' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'DD-MM-YYYY' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Success case - Date test - Format MM-DD-YYYY', () => { + const req = { + body: { + page: { + sorted: '08-18-2019' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'MM-DD-YYYY' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Success case - length test', () => { - const req = { - body: { - page: { - sorted: '33' + + test('Test nested objects Success case - Date test - Format MM/DD/YYYY', () => { + const req = { + body: { + page: { + sorted: '12/18/2019' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, length: { min: 2, max: 5 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - length test', () => { - const req = { - body: { - page: { - sorted: '34334344' + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'MM/DD/YYYY' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Success case - Date test - Format DD/MM/YYYY', () => { + const req = { + body: { + page: { + sorted: '12/07/2019' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, length: { min: 2, max: 5 } }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'DD/MM/YYYY' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Success case - Date test - Format YYYY/MM/DD', () => { + const req = { + body: { + page: { + sorted: '2901/07/19' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate: true, format: 'YYYY/MM/DD' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested objects Success case - includes test', () => { - const req = { - body: { - page: { - sorted: 'enabled' + + test('Test nested objects Error case - Date test', () => { + const req = { + body: { + page: { + sorted: 'not-a-valid-date' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, includes: [ 'enabled', 'disabled' ] }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Error case - includes test', () => { - const req = { - body: { - page: { - sorted: '34334344' + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate : true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Error case - Date test - Format', () => { + const req = { + body: { + page: { + sorted: '09-08-3045' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, includes: [ 'enabled', 'disabled' ] }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY-MM-DD' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Error case - Date test - Invalid month/date', () => { + const req = { + body: { + page: { + sorted: '2222-89-45' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY-MM-DD' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); - }); - - test('Test nested objects Success case - excludes test', () => { - const req = { - body: { - page: { - sorted: '34334344' + + test('Test nested objects Error case - Date test - Invalid Format', () => { + const req = { + body: { + page: { + sorted: '2019-23-02' + } } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, excludes: [ 'enabled', 'disabled' ] }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, isDate : true, format: 'YYYY/DD/MM' }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); }); - test('Test nested objects Error case - excludes test', () => { - const req = { - body: { - page: { - sorted: 'enabled' - } - } - }; - const validation = [ - {param : 'page', location : 'body', isObject : true, children : [ - {param : 'sorted', location : 'body.page', isRequired : true, excludes: [ 'enabled', 'disabled' ] }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'sorted', + describe('Mobile number test', () => { + test('Test nested objects Success case - Mobile number test', () => { + const req = { + body: { + page: { + sorted: '919035803903' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - test('Test nested objects Failure case - Array test', () => { - const req = { - body: { - page: [ - 1, 2, 3, 'sdsd' - ] - } - }; - const validation = [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isNumber: true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'page-array', + + test('Test nested objects Success case - Mobile number test - country code not mandatory', () => { + const req = { + body: { + page: { + sorted: '919035803903' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : false, length: {min : 1, max : 10}} }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); }); - }); - test('Test nested objects Success case - Array test', () => { - const req = { - body: { - page: [ - 1, 2, 3 - ] - } - }; - const validation = [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isNumber: true }, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - }); - test('Test nested objects Failure case - Array test 2', () => { - const req = { - body: { - page: [ - {test: 1, test2: 2}, - {test: 1, test2: 2}, - {test: 1, test2: 'test'} + + test('Test nested objects Error case - Mobile number test - country code mandatory', () => { + const req = { + body: { + page: { + sorted: '9035803903' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } ] - } - }; - const validation = [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isObject: true, children: [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, - ]}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'test2', + }); + }); + + test('Test nested objects Error case - Mobile number test - length range min', () => { + const req = { + body: { + page: { + sorted: '919035844' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 8, max : 10}} }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); - }); - test('Test nested objects Success case - Array test 2', () => { - const req = { - body: { - page: [ - {test: 1, test2: 2}, - {test: 1, test2: 2}, - {test: 1, test2: 323} + + test('Test nested objects Error case - Mobile number test - length range max', () => { + const req = { + body: { + page: { + sorted: '9190358444444' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 8, max : 10}} }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } ] - } - }; - const validation = [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isObject: true, children: [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, - ]}, + }); + }); + + test('Test nested objects Error case - Mobile number test', () => { + const req = { + body: { + page: { + sorted: 'not-a-valid-mobile number' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, mobileNumber: {countryCode : '91', isCountryCodeMandatory : true, length: {min : 1, max : 10}} }, ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - }); - - test('Test nested objects Failure case - Array test 3 multiple errors', () => { - const req = { - body: { - page: [ - {test: 1, test2: 2}, - {test: 1, test2: 2}, - {test: 1, test2: 'test'} - ], - page1: [ - {test: 1, test2: 2}, - {test: 1, test2: 2}, - {test: 1, test2: 'test'} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } ] - } - }; - const validation = [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isObject: true, children: [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, - ]}, - ]}, - {param : 'page1', location : 'body', isArray : true, children : [ - {param: 'page1-array', isRequired : true, isObject: true, children: [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, - ]}, - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'test2', - }, - { - location: 'body', - message: 'Invalid Field Error', - param: 'test2', - } - ] + }); }); }); - test('Test nested objects Failure case - Object test', () => { - const req = { - body: { - pageHead: { - page: [ - {test: 1, test2: 2}, - {test: 1, test2: 2}, - {test: 1, test2: 'test'} - ] + describe('Length test', () => { + test('Test nested objects Success case - length test', () => { + const req = { + body: { + page: { + sorted: '33' + } } - } - }; - const validation = [ - {param : 'pageHead', location : 'body', isObject : true, children : [ - {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isObject: true, children: [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, - ]}, + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, length: { min: 2, max: 5 } }, ]} - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'test2', + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - length test', () => { + const req = { + body: { + page: { + sorted: '34334344' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, length: { min: 2, max: 5 } }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); }); - test('Test nested objects Failure case - Object test 2', () => { - const req = { - body: { - pageHead: { + describe('Includes and Excludes test', () => { + test('Test nested objects Success case - includes test', () => { + const req = { + body: { page: { - test: 1, test2: 'test' + sorted: 'enabled' } } - } - }; - const validation = [ - {param : 'pageHead', location : 'body', isObject : true, children : [ + }; + const validation = [ {param : 'page', location : 'body', isObject : true, children : [ - {param: 'test', isNumber: true}, - {param: 'test2', isNumber: true}, + {param : 'sorted', location : 'body.page', isRequired : true, includes: [ 'enabled', 'disabled' ] }, ]} - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'test2', + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - includes test', () => { + const req = { + body: { + page: { + sorted: '34334344' + } } - ] + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, includes: [ 'enabled', 'disabled' ] }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); + }); + + test('Test nested objects Success case - excludes test', () => { + const req = { + body: { + page: { + sorted: '34334344' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, excludes: [ 'enabled', 'disabled' ] }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Error case - excludes test', () => { + const req = { + body: { + page: { + sorted: 'enabled' + } + } + }; + const validation = [ + {param : 'page', location : 'body', isObject : true, children : [ + {param : 'sorted', location : 'body.page', isRequired : true, excludes: [ 'enabled', 'disabled' ] }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'sorted', + } + ] + }); }); }); - - test('Test nested Arrays Failure case - Array of arrays 1', () => { - const req = { - body: { - pageHead: { + + describe('Arrays test', () => { + test('Test nested objects Failure case - Array test', () => { + const req = { + body: { page: [ - [1, 2, 3.], - {a: 1} + 1, 2, 3, 'sdsd' ] } - } - }; - const validation = [ - {param : 'pageHead', location : 'body', isObject : true, children : [ + }; + const validation = [ {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isArray: true, children: [ - {param: 'page-child-array', isRequired : true, isNumber: true } - ]} + {param: 'page-array', isRequired : true, isNumber: true }, ]} - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'page-array', + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'page-array', + } + ] + }); + }); + test('Test nested objects Success case - Array test', () => { + const req = { + body: { + page: [ + 1, 2, 3 + ] } - ] + }; + const validation = [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isNumber: true }, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); }); - }); - - test('Test nested Arrays Failure case - Array of arrays 2', () => { - const req = { - body: { - pageHead: { + test('Test nested objects Failure case - Array test 2', () => { + const req = { + body: { page: [ - [1, 2, 3, 'string'], + {test: 1, test2: 2}, + {test: 1, test2: 2}, + {test: 1, test2: 'test'} ] } - } - }; - const validation = [ - {param : 'pageHead', location : 'body', isObject : true, children : [ + }; + const validation = [ {param : 'page', location : 'body', isArray : true, children : [ - {param: 'page-array', isRequired : true, isArray: true, children: [ - {param: 'page-child-array', isRequired : true, isNumber: true } + {param: 'page-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'test2', + } + ] + }); + }); + test('Test nested objects Success case - Array test 2', () => { + const req = { + body: { + page: [ + {test: 1, test2: 2}, + {test: 1, test2: 2}, + {test: 1, test2: 323} + ] + } + }; + const validation = [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + }); + + test('Test nested objects Failure case - Array test 3 multiple errors', () => { + const req = { + body: { + page: [ + {test: 1, test2: 2}, + {test: 1, test2: 2}, + {test: 1, test2: 'test'} + ], + page1: [ + {test: 1, test2: 2}, + {test: 1, test2: 2}, + {test: 1, test2: 'test'} + ] + } + }; + const validation = [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]}, + ]}, + {param : 'page1', location : 'body', isArray : true, children : [ + {param: 'page1-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true}, + ]}, + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'test2', + }, + { + location: 'body', + message: 'Invalid Field Error', + param: 'test2', + } + ] + }); + }); + test('Test nested Arrays Failure case - Array of arrays 1', () => { + const req = { + body: { + pageHead: { + page: [ + [1, 2, 3.], + {a: 1} + ] + } + } + }; + const validation = [ + {param : 'pageHead', location : 'body', isObject : true, children : [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isArray: true, children: [ + {param: 'page-child-array', isRequired : true, isNumber: true } + ]} ]} ]} - ]} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('body', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(0); - expect(resp.status).toHaveBeenCalledTimes(1); - expect(resp.send).toHaveBeenCalledWith({ - error: [ - { - location: 'body', - message: 'Invalid Field Error', - param: 'page-child-array', + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'page-array', + } + ] + }); + }); + + test('Test nested Arrays Failure case - Array of arrays 2', () => { + const req = { + body: { + pageHead: { + page: [ + [1, 2, 3, 'string'], + ] + } } - ] + }; + const validation = [ + {param : 'pageHead', location : 'body', isObject : true, children : [ + {param : 'page', location : 'body', isArray : true, children : [ + {param: 'page-array', isRequired : true, isArray: true, children: [ + {param: 'page-child-array', isRequired : true, isNumber: true } + ]} + ]} + ]} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('body', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(0); + expect(resp.status).toHaveBeenCalledTimes(1); + expect(resp.send).toHaveBeenCalledWith({ + error: [ + { + location: 'body', + message: 'Invalid Field Error', + param: 'page-child-array', + } + ] + }); }); }); - test('Test Success case - Required test - path headers', () => { - const req = { - headers: { - Authorization: 'Bearer testauth' - } - }; - const validation = [ - {param : 'Authorization', location : 'headers', isRequired : true} - ]; - const response = { - mode: 'reject' - }; - const validatorfn = validaor('headers', validation, response); - validatorfn(req, resp, next); - expect(next).toHaveBeenCalledTimes(1); - expect(resp.status).toHaveBeenCalledTimes(0); - expect(resp.send).toHaveBeenCalledTimes(0); + describe('Header test', () => { + test('Test Success case - Required test - path headers', () => { + const req = { + headers: { + Authorization: 'Bearer testauth' + } + }; + const validation = [ + {param : 'Authorization', location : 'headers', isRequired : true} + ]; + const response = { + mode: 'reject' + }; + const validatorfn = validaor('headers', validation, response); + validatorfn(req, resp, next); + expect(next).toHaveBeenCalledTimes(1); + expect(resp.status).toHaveBeenCalledTimes(0); + expect(resp.send).toHaveBeenCalledTimes(0); + }); }); }); diff --git a/package.json b/package.json index 408f581..cff8fac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "expressjs-field-validator", - "version": "3.0.3", + "version": "3.0.4", "description": "Plugin for validating field values of json request in expressjs", "homepage": "https://gsmithun4.github.io/expressjs-field-validator", "repository": {