Skip to content

Commit

Permalink
Merge pull request #60 from shystruk/dev
Browse files Browse the repository at this point in the history
check config, webpack source map
  • Loading branch information
shystruk authored May 28, 2021
2 parents ef14e74 + 741a17d commit 41460c9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
3 changes: 2 additions & 1 deletion demo/dist/bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions demo/dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const isPasswordValid = function (password) {
const CSVConfig = {
headers: [
{ name: 'First Name', inputName: 'firstName', required: true, requiredError },
{ name: 'Last Name', inputName: 'lastName', required: true, requiredError },
{ name: 'Last Name', inputName: 'lastName', required: true, requiredError, optional: true },
{ name: 'Email', inputName: 'email', required: true, requiredError, unique: true, uniqueError, validate: isEmailValid, validateError },
{ name: 'Password', inputName: 'password', required: true, requiredError, validate: isPasswordValid, validateError },
{ name: 'Roles', inputName: 'roles', required: true, requiredError, isArray: true }
Expand Down
11 changes: 6 additions & 5 deletions demo/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path');

module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
entry: './index.js',
devtool: 'source-map',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
24 changes: 15 additions & 9 deletions src/csv-file-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
*/
function CSVFileValidator(csvFile, config) {
return new Promise(function (resolve, reject) {
if (!config || (config && !config.headers)) {
return resolve({
inValidMessages: ['config headers are required'],
data: []
});
}

Papa.parse(csvFile, {
skipEmptyLines: true,
complete: function (results) {
Expand All @@ -36,23 +43,22 @@
* @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 = {};
const headers = [];

for (let i = 0; i < config.headers.length; i++) {
const data = config.headers[i];

if (!data.optional) {
headers.push(data);
}
}

// Skip the row if not enough columns or .csv formatting is wrong
if (row.length < headers.length) {
return;
}
Expand Down
14 changes: 11 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ test('module should be a function', t => {
t.is(typeof CSVFileValidator, 'function');
});

test('should return an object with empty inValidMessages/data keys', async t => {
const csvData = await CSVFileValidator('', {});
test('should return the message "config headers are required"', async t => {
const csvData = await CSVFileValidator('');

t.is(typeof csvData, 'object');
t.deepEqual(csvData.inValidMessages, ['config headers are required']);
t.deepEqual(csvData.data, []);
});

test('should return no data if the file is empty', async t => {
const csvData = await CSVFileValidator('', CSVConfig);

t.is(typeof csvData, 'object');
t.deepEqual(csvData.inValidMessages, []);
Expand All @@ -73,7 +81,7 @@ test('should return invalid messages with data', async t => {
t.is(csvData.data.length, 2);
});

test('should return data, file is valid', async t => {
test('should return data, the file is valid', async t => {
const csvData = await CSVFileValidator(CSVValidFile, CSVConfig);

t.is(csvData.inValidMessages.length, 0);
Expand Down

0 comments on commit 41460c9

Please sign in to comment.