Skip to content

Commit

Permalink
Merge pull request #62 from shystruk/dev
Browse files Browse the repository at this point in the history
fields are mismatch
  • Loading branch information
shystruk authored Jun 2, 2021
2 parents 41460c9 + 649904c commit e50646d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion demo/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csv-file-validator",
"version": "1.10.3",
"version": "1.10.4",
"description": "Validation of CSV file against user defined schema (returns back object with data and invalid messages)",
"main": "./src/csv-file-validator.js",
"repository": "https://github.com/shystruk/csv-file-validator.git",
Expand Down
16 changes: 6 additions & 10 deletions src/csv-file-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,20 @@
* @private
*/
function _prepareDataAndValidateFile(csvData, config) {
const headers = [];
const file = {
inValidMessages: [],
data: []
};

for (let i = 0; i < config.headers.length; i++) {
if (!config.headers[i].optional) {
headers.push(config.headers[i]);
}
}

csvData.forEach(function (row, rowIndex) {
const columnData = {};

// Skip the row if not enough columns or .csv formatting is wrong
if (row.length < headers.length) {
return;
// fields are mismatch
if (rowIndex !== 0 && row.length > config.headers.length) {
file.inValidMessages.push(
'Too many fields: expected ' + config.headers.length + ' fields' +
' but parsed ' + row.length + '. In the row ' + rowIndex
);
}

row.forEach(function (columnValue, columnIndex) {
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const CSVInvalidFileWithDuplicates = [
'Vasyl;Stokolosa;[email protected];123123123;user;Ukraine',
].join('\n');

const CSVInvalidFileFieldsMismatch = [
'First Name;',
'Vasyl;Stokolosa;',
].join('\n');

test('module should be a function', t => {
t.is(typeof CSVFileValidator, 'function');
});
Expand Down Expand Up @@ -121,3 +126,10 @@ test('file is valid and Email is not unique at the ... row', async t => {
t.is(csvData.inValidMessages.length, 2);
t.is(csvData.data.length, 3);
});

test('fields are mismatch', async t => {
const csvData = await CSVFileValidator(CSVInvalidFileFieldsMismatch, { headers: [CSVConfig.headers[0]] });

t.is(csvData.inValidMessages.length, 1);
t.is(csvData.data.length, 1);
});

0 comments on commit e50646d

Please sign in to comment.