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": { 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", diff --git a/src/csv-file-validator.js b/src/csv-file-validator.js index adc6772..8a39193 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); +});