Skip to content

Commit e50646d

Browse files
authored
Merge pull request #62 from shystruk/dev
fields are mismatch
2 parents 41460c9 + 649904c commit e50646d

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

demo/dist/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/dist/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "csv-file-validator",
3-
"version": "1.10.3",
3+
"version": "1.10.4",
44
"description": "Validation of CSV file against user defined schema (returns back object with data and invalid messages)",
55
"main": "./src/csv-file-validator.js",
66
"repository": "https://github.com/shystruk/csv-file-validator.git",

src/csv-file-validator.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,20 @@
4343
* @private
4444
*/
4545
function _prepareDataAndValidateFile(csvData, config) {
46-
const headers = [];
4746
const file = {
4847
inValidMessages: [],
4948
data: []
5049
};
5150

52-
for (let i = 0; i < config.headers.length; i++) {
53-
if (!config.headers[i].optional) {
54-
headers.push(config.headers[i]);
55-
}
56-
}
57-
5851
csvData.forEach(function (row, rowIndex) {
5952
const columnData = {};
6053

61-
// Skip the row if not enough columns or .csv formatting is wrong
62-
if (row.length < headers.length) {
63-
return;
54+
// fields are mismatch
55+
if (rowIndex !== 0 && row.length > config.headers.length) {
56+
file.inValidMessages.push(
57+
'Too many fields: expected ' + config.headers.length + ' fields' +
58+
' but parsed ' + row.length + '. In the row ' + rowIndex
59+
);
6460
}
6561

6662
row.forEach(function (columnValue, columnIndex) {

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ const CSVInvalidFileWithDuplicates = [
5454
'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine',
5555
].join('\n');
5656

57+
const CSVInvalidFileFieldsMismatch = [
58+
'First Name;',
59+
'Vasyl;Stokolosa;',
60+
].join('\n');
61+
5762
test('module should be a function', t => {
5863
t.is(typeof CSVFileValidator, 'function');
5964
});
@@ -121,3 +126,10 @@ test('file is valid and Email is not unique at the ... row', async t => {
121126
t.is(csvData.inValidMessages.length, 2);
122127
t.is(csvData.data.length, 3);
123128
});
129+
130+
test('fields are mismatch', async t => {
131+
const csvData = await CSVFileValidator(CSVInvalidFileFieldsMismatch, { headers: [CSVConfig.headers[0]] });
132+
133+
t.is(csvData.inValidMessages.length, 1);
134+
t.is(csvData.data.length, 1);
135+
});

0 commit comments

Comments
 (0)