Skip to content

Commit

Permalink
Merge pull request #83 from shystruk/dev
Browse files Browse the repository at this point in the history
fields are mismatch. v1.12.0
  • Loading branch information
shystruk authored Sep 16, 2021
2 parents f6055bc + 1711030 commit 67a0699
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
6 changes: 3 additions & 3 deletions demo-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.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",
Expand Down
4 changes: 2 additions & 2 deletions src/csv-file-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
29 changes: 22 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
Expand Down Expand Up @@ -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);
});

0 comments on commit 67a0699

Please sign in to comment.