From f29b276df9a5f6a0f316ead936324691b4082583 Mon Sep 17 00:00:00 2001 From: Anastasii Tovstyk Date: Wed, 28 Jul 2021 15:48:02 +0200 Subject: [PATCH 1/3] Fixes #63: Improve number of fields check --- src/csv-file-validator.js | 4 ++-- test.js | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/csv-file-validator.js b/src/csv-file-validator.js index 15da5fa..741d374 100644 --- a/src/csv-file-validator.js +++ b/src/csv-file-validator.js @@ -52,9 +52,9 @@ const columnData = {}; // fields are mismatch - if (rowIndex !== 0 && row.length > config.headers.length) { + if (rowIndex !== 0 && row.length !== config.headers.length) { file.inValidMessages.push( - 'Too many fields: expected ' + config.headers.length + ' fields' + + 'Number of fields mismatch: expected ' + config.headers.length + ' fields' + ' but parsed ' + row.length + '. In the row ' + rowIndex ); } diff --git a/test.js b/test.js index 38c1613..54abc65 100644 --- a/test.js +++ b/test.js @@ -32,13 +32,13 @@ const CSVHeader = CSVConfig.headers.map(i => i.name).join(';'); const CSVInvalidFile = [ CSVHeader, - 'Vasyl;Stokolosa;v.stokol@gmail.com;123;admin,manager', - 'Vasyl_2;"";v.stokol@gmail.com;123123123;user', + 'Vasyl;Stokolosa;v.stokol@gmail.com;123;admin,manager;', + 'Vasyl_2;"";v.stokol@gmail.com;123123123;user;', ].join('\n'); const CSVValidFile = [ CSVHeader, - 'Vasyl;Stokolosa;v.stokol@gmail.com;123123;admin,manager', + 'Vasyl;Stokolosa;v.stokol@gmail.com;123123;admin,manager;', 'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine', ].join('\n'); @@ -49,16 +49,22 @@ const CSVValidFileWithoutHeaders = [ const CSVInvalidFileWithDuplicates = [ CSVHeader, - 'Vasyl;Stokolosa;fake@test.com;123123;admin,manager', + 'Vasyl;Stokolosa;fake@test.com;123123;admin,manager;', 'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine', 'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine', ].join('\n'); -const CSVInvalidFileFieldsMismatch = [ +const CSVInvalidFileTooManyFields = [ 'First Name;', 'Vasyl;Stokolosa;', ].join('\n'); +const CSVInvalidFileNotEnoughFields = [ + 'Country;First Name;Last Name;', + '"";Vasyl', + '"";Vasyl;Stokolosa', +].join('\n'); + test('module should be a function', t => { t.is(typeof CSVFileValidator, 'function'); }); @@ -127,9 +133,18 @@ test('file is valid and Email is not unique at the ... row', async t => { t.is(csvData.data.length, 3); }); -test('fields are mismatch', async t => { - const csvData = await CSVFileValidator(CSVInvalidFileFieldsMismatch, { headers: [CSVConfig.headers[0]] }); +test('fields are mismatch: too many fields', async t => { + const csvData = await CSVFileValidator(CSVInvalidFileTooManyFields, { headers: [CSVConfig.headers[0]] }); t.is(csvData.inValidMessages.length, 1); + t.is(csvData.inValidMessages[0], "Number of fields mismatch: expected 1 fields but parsed 3. In the row 1") t.is(csvData.data.length, 1); }); + +test('fields are mismatch: not enough fields', async t => { + const csvData = await CSVFileValidator(CSVInvalidFileNotEnoughFields, { headers: [CSVConfig.headers[5], CSVConfig.headers[0], CSVConfig.headers[1]] }); + + t.is(csvData.inValidMessages.length, 1); + t.is(csvData.inValidMessages[0], "Number of fields mismatch: expected 3 fields but parsed 2. In the row 1"); + t.is(csvData.data.length, 2); +}); From 056261e033a27564f112befd3bc1b0430b90e17b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 1 Sep 2021 22:06:12 +0000 Subject: [PATCH 2/3] Bump path-parse from 1.0.6 to 1.0.7 in /demo-ts Bumps [path-parse](https://github.com/jbgutierrez/path-parse) from 1.0.6 to 1.0.7. - [Release notes](https://github.com/jbgutierrez/path-parse/releases) - [Commits](https://github.com/jbgutierrez/path-parse/commits/v1.0.7) --- updated-dependencies: - dependency-name: path-parse dependency-type: indirect ... Signed-off-by: dependabot[bot] --- demo-ts/package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demo-ts/package-lock.json b/demo-ts/package-lock.json index f663bc9..70d69c8 100644 --- a/demo-ts/package-lock.json +++ b/demo-ts/package-lock.json @@ -757,9 +757,9 @@ "dev": true }, "path-parse": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "dev": true }, "picomatch": { From 171103064efc4ac6fc616e8c3da7b4c43c10b72d Mon Sep 17 00:00:00 2001 From: shystruk Date: Thu, 16 Sep 2021 00:11:18 -0700 Subject: [PATCH 3/3] 1.12.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6441cf4..8478145 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "csv-file-validator", - "version": "1.11.1", + "version": "1.12.0", "description": "Validation of CSV file against user defined schema (returns back object with data and invalid messages)", "main": "./src/csv-file-validator.js", "types": "./src/csv-file-validator.d.ts",