From 779c9dce86b4826ca0c8f39b0cc08054cfc1d68d Mon Sep 17 00:00:00 2001 From: Midhun G S Date: Tue, 1 Aug 2023 12:47:18 +0530 Subject: [PATCH] Merge to main (#24) * added test cases * remove coverage from npm * bump version * test case fix --- .npmignore | 1 + lib/queryBuilder/queryBuilder.test.js | 44 +++++++++++++++++++++++++++ lib/validator/validator.test.js | 34 +++++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/.npmignore b/.npmignore index 0fe4c40..a2aff2d 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,7 @@ .git /node_modules /example +/coverage /.github /lib/validator/validator.js /lib/validator/validator.test.js diff --git a/lib/queryBuilder/queryBuilder.test.js b/lib/queryBuilder/queryBuilder.test.js index 88698c3..dbb3bff 100644 --- a/lib/queryBuilder/queryBuilder.test.js +++ b/lib/queryBuilder/queryBuilder.test.js @@ -151,6 +151,50 @@ describe('Test for validateBody', () => { }], {} ); }); + test('validateBody validator object isArray and object child nested - 1', () => { + validateBody().addParams([ + param('test').isArray().addChildren([ + param('child1').isObject().addChildren([ + param('param1').isRequired().end(), + param('param2').isNumber().end() + ]).end() + ]).end() + ]).done(); + expect(validator).toBeCalledWith('body', + [{ + param: 'test', + isArray: true, + children: [ + { param: 'child1', isObject: true, children: [ + { param: 'param1', isRequired: true}, + { param: 'param2', isNumber: true}, + ]} + ] + }], {} + ); + }); + test('validateBody validator object isArray and object child nested - 2', () => { + validateBody().addParams([ + param('test').isArray().addChild( + param('child1').isObject().addChildren([ + param('param1').isRequired().end(), + param('param2').isNumber().end() + ]).end() + ).end() + ]).done(); + expect(validator).toBeCalledWith('body', + [{ + param: 'test', + isArray: true, + children: [ + { param: 'child1', isObject: true, children: [ + { param: 'param1', isRequired: true}, + { param: 'param2', isNumber: true}, + ]} + ] + }], {} + ); + }); test('validateBody validator object shouldInclude', () => { validateBody().addParams([ param('test').shouldInclude(['a','b']).end() diff --git a/lib/validator/validator.test.js b/lib/validator/validator.test.js index d696a68..0fdee10 100644 --- a/lib/validator/validator.test.js +++ b/lib/validator/validator.test.js @@ -1660,6 +1660,40 @@ describe('Test for body params', () => { ] }); }); + test('Test nested objects - object in array required check', () => { + const req = { + body: { + page1: [ + {test: 1, test2: 2}, + {test: 1}, + ] + } + }; + const validation = [ + {param : 'page1', location : 'body', isArray : true, children : [ + {param: 'page1-array', isRequired : true, isObject: true, children: [ + {param: 'test', isNumber: true}, + {param: 'test2', isNumber: true, isRequired: 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 Arrays Failure case - Array of arrays 1', () => { const req = { body: { diff --git a/package-lock.json b/package-lock.json index 7346ab1..18f485f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "expressjs-field-validator", - "version": "3.0.5", + "version": "3.0.6", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 9cfb501..0cf17db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "expressjs-field-validator", - "version": "3.0.5", + "version": "3.0.6", "description": "Plugin for validating field values of json request in expressjs", "homepage": "https://gsmithun4.github.io/expressjs-field-validator", "repository": {