From 44ae30e462dec463048df0b4fd82c6211fcae355 Mon Sep 17 00:00:00 2001 From: gsmithun4 Date: Tue, 21 Dec 2021 04:33:53 +0530 Subject: [PATCH] Fix --- README.md | 21 +++-- lib/validator/validator.js | 24 +++-- lib/validator/validator.test.js | 150 ++++++++++++++++++++++++++++---- package.json | 2 +- 4 files changed, 158 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index faee6d0..f0a0266 100644 --- a/README.md +++ b/README.md @@ -300,12 +300,16 @@ Ends a validation definition { "field1": "Value", // String, Mandatory "field2": [ // array, Mandatory - { "field21": "44443" } // Number Optional + { "field21": "44443" }, // object Optional, number mandatory + { "field21": "44443" } ], "field3": { // Object Optional - { "field31": "true" }, // Boolean Mandatory - { "field32": "String" } // String Mandatory - } + "field31": "true", // Boolean Mandatory + "field32": "String" // String Mandatory + }, + "field4": [ // array, Mandatory + 123, 445, 3434 // Number Optional + ], } ``` Should send http status code 500 in case of error @@ -315,12 +319,17 @@ router.post('/users/:id', validateBody().isToBeRejected().sendErrorCode(500).addParams([ param('field1').isRequired().end(), param('field2').isRequired().isArray().isRequired().addChild( - param('field21').isNumber().end() + param('field2-array').isObject().addChild( // field2-array is for tracking, you can give any name here + param('field21').isNumber().isRequired().end() + ).end() ).end(), - param('field3').isRequired().isObject().addChildren([ + param('field3').isObject().addChildren([ param('field31').isBoolean().isRequired().end(), param('field32').isRequired().end() ]).end(), + param('field4').isRequired().isArray().isRequired().addChild( + param('field4-array').isNumber().end() + ).end(), ]).done(), (req, res, next) => { diff --git a/lib/validator/validator.js b/lib/validator/validator.js index c84c19d..24be686 100644 --- a/lib/validator/validator.js +++ b/lib/validator/validator.js @@ -39,7 +39,7 @@ module.exports = (location = 'body', validation = [], response = {}) => { return false; }; const checkFormat = (value, validateObj) => { - const { param, location, isRequired, isNumber, isEmail, isBoolean, + const { param, isRequired, isNumber, isEmail, isBoolean, length, message, isDate, format, isArray, isObject, range, includes, excludes, mobileNumber } = validateObj; @@ -215,21 +215,19 @@ module.exports = (location = 'body', validation = [], response = {}) => { }); }; const getChildArrayErrors = (child, value) => { + const arrayType = child[0]; return value.map(v => { - return child.map(c => { - const subvalue = getValue(v, c.param); - const err = checkFormat(subvalue, c); - if (err) - return [err]; + const err = checkFormat(v, arrayType); + if (err) + return [err]; - if (subvalue && c.children) { - if (c.isArray) { - return getChildArrayErrors(c.children, subvalue); - } else if (c.isObject) { - return getChildErrors(c.children, subvalue); - } + if (v && arrayType.children) { + if (arrayType.isArray) { + return getChildArrayErrors(arrayType.children, v); + } else if (arrayType.isObject) { + return getChildErrors(arrayType.children, v); } - }); + } }); }; const value = getValue(); diff --git a/lib/validator/validator.test.js b/lib/validator/validator.test.js index ee83f47..6305d5c 100644 --- a/lib/validator/validator.test.js +++ b/lib/validator/validator.test.js @@ -57,7 +57,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -236,7 +236,7 @@ describe('Test for body params', () => { expect(req.locals.data).toEqual({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -267,7 +267,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -299,7 +299,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -330,7 +330,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Mandatory field sorted missing', param: 'sorted', } @@ -361,7 +361,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -434,7 +434,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -488,7 +488,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -542,7 +542,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -711,7 +711,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -742,7 +742,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -773,7 +773,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -850,7 +850,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -881,7 +881,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -912,7 +912,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -943,7 +943,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -997,7 +997,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -1051,7 +1051,7 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } @@ -1105,11 +1105,123 @@ describe('Test for body params', () => { expect(resp.send).toHaveBeenCalledWith({ error: [ { - location: 'body.page', + location: 'body', message: 'Invalid Field Error', param: 'sorted', } ] }); }); + 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 - 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'} + ] + } + }; + 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 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); + }); }); diff --git a/package.json b/package.json index 0512f04..2299b32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "expressjs-field-validator", - "version": "3.0.0", + "version": "3.0.2", "description": "Plugin for validating field values of json request in expressjs", "homepage": "https://gsmithun4.github.io/expressjs-field-validator", "repository": {