Skip to content

Commit

Permalink
Merge pull request #129 from shystruk/improvements
Browse files Browse the repository at this point in the history
Add dependentValidateError. Upgrade papaparse
  • Loading branch information
shystruk authored Dec 10, 2024
2 parents 0f6c355 + 2d385ac commit 1175c73
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 30 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ function(email, row) {
}
```

### dependentValidateError
Type: `Function` <br>

If dependentValidate returns false dependentValidateError function will be called with arguments **headerName, rowNumber, columnNumber**

### isArray
Type: `Boolean` <br>

Expand Down
10 changes: 5 additions & 5 deletions demo-ts/dist/bundle.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions demo/demo.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
First Name;Last Name;Email;Password;Roles
Vasyl;Stokolosa;api01@test.com;123;admin, manager
Vasyl_2;Stokolosa_2;api01@test.com;123123123;admin, manager, user
;Stokolosa_2;api@test;123123123;admin, manager, user
First Name;Last Name;Email;Password;Roles;Country
Vasyl;Stokolosa;api01@test.com;123;admin, manager;Ukraine
Vasyl_2;Stokolosa_2;api01@test.com;123123123;admin, manager, user;USA
;Stokolosa_2;api@test;123123123;user;Ukraine
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.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @license
Papa Parse
v5.3.2
v5.4.1
https://github.com/mholt/PapaParse
License: MIT
*/
2 changes: 1 addition & 1 deletion demo/dist/bundle.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ const validateError = (headerName, rowNumber, columnNumber) => {
return `<div class="red">${headerName} is not valid in the <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
}

const dependentValidateError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is not valid. Country should be set to Ukraine or role is not user. <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)

const isRoleForCountryValid = (country, row) => {
const role = row[4];
return country === 'Ukraine' && role === 'user';
}

const uniqueError = (headerName, rowNumber) => {
return `<div class="red">${headerName} is not unique at the <strong>${rowNumber} row</strong></div>`
}
Expand All @@ -31,7 +40,8 @@ const CSVConfig = {
{ 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 }
{ name: 'Roles', inputName: 'roles', required: true, requiredError, isArray: true },
{ name: 'Country', inputName: 'country', optional: true, dependentValidate: isRoleForCountryValid, dependentValidateError }
],
isColumnIndexAlphabetic: true
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csv-file-validator",
"version": "2.1.0",
"version": "2.2.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 Expand Up @@ -34,10 +34,10 @@
"dependencies": {
"famulus": "^2.2.3",
"lodash": "^4.17.21",
"papaparse": "^5.3.2"
"papaparse": "^5.4.1"
},
"devDependencies": {
"@types/papaparse": "^5.3.2",
"@types/papaparse": "^5.3.15",
"ava": "^0.25.0",
"codecov.io": "^0.1.6",
"nyc": "^11.4.1",
Expand Down
13 changes: 10 additions & 3 deletions src/csv-file-validator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,25 @@ export interface FieldSchema {
*/
validate?: (field: string|number|boolean) => boolean;

/**
* If validate returns false validateError function
* will be called with arguments headerName, rowNumber, columnNumber.
*/
validateError?: (headerName: string, rowNumber: number,
columnNumber: number) => string;

/**
* Validate column value that depends on other values in other columns.
* Must return true for valid field and false for invalid.
*/
dependentValidate?: (field: string, row: [string]) => boolean;

/**
* If validate returns false validateError function
* If dependentValidate returns false dependentValidateError function
* will be called with arguments headerName, rowNumber, columnNumber.
*/
validateError?: (headerName: string, rowNumber: number,
columnNumber: number) => string;
dependentValidateError?: (headerName: string, rowNumber: number,
columnNumber: number) => string;
}

export interface RowError {
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 @@ -129,8 +129,8 @@
file.inValidData.push({
rowIndex: rowIndex + 1,
columnIndex: columnIndex,
message: _isFunction(valueConfig.validateError)
? valueConfig.validateError(valueConfig.name, rowIndex + 1, columnIndex)
message: _isFunction(valueConfig.dependentValidateError)
? valueConfig.dependentValidateError(valueConfig.name, rowIndex + 1, columnIndex)
: String(valueConfig.name + ' not passed dependent validation in the ' + (rowIndex + 1) + ' row / ' + (columnIndex + 1) + ' column')
});
}
Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const validateError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is not valid in the <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)

const dependentValidateError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is not valid. Country should be set to Ukraine. <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)

const isEmailValid = (email) => {
const reqExp = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/
return reqExp.test(email)
Expand All @@ -29,7 +33,7 @@ const CSVConfig = {
{ 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 },
{ name: 'Country', inputName: 'country', optional: true, dependentValidate: isRoleForCountryValid }
{ name: 'Country', inputName: 'country', optional: true, dependentValidate: isRoleForCountryValid, dependentValidateError }
]
}

Expand Down Expand Up @@ -103,6 +107,9 @@ test('should return invalid messages with data', async t => {

t.is(csvData.inValidData.length, 5);
t.is(csvData.data.length, 2);
t.is(csvData.inValidData[3].message,
'<div class="red">Country is not valid. Country should be set to Ukraine. <strong>3 row</strong> / <strong>6 column</strong></div>'
);
});

test('should return data, the file is valid', async t => {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
integrity sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==

"@types/papaparse@^5.3.2":
version "5.3.2"
resolved "https://registry.yarnpkg.com/@types/papaparse/-/papaparse-5.3.2.tgz#6ccace6eac8ddb03a6fd06883b84dd6c6561f69f"
integrity sha512-BNbCHJkTE4RwmAFkCxEalET4mDvGr/1ld7ZtQ4i/laWI/iiVt+GL07stdvufle4KfywyvloqqpIiJscXNCrKxA==
"@types/papaparse@^5.3.15":
version "5.3.15"
resolved "https://registry.yarnpkg.com/@types/papaparse/-/papaparse-5.3.15.tgz#7cafa16757a1d121422deefbb10b6310b224ecc4"
integrity sha512-JHe6vF6x/8Z85nCX4yFdDslN11d+1pr12E526X8WAfhadOeaOTx5AuIkvDKIBopfvlzpzkdMx4YyvSKCM9oqtw==
dependencies:
"@types/node" "*"

Expand Down Expand Up @@ -3312,10 +3312,10 @@ package-json@^4.0.0:
registry-url "^3.0.3"
semver "^5.1.0"

papaparse@^5.3.2:
version "5.3.2"
resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.3.2.tgz#d1abed498a0ee299f103130a6109720404fbd467"
integrity sha512-6dNZu0Ki+gyV0eBsFKJhYr+MdQYAzFUGlBMNj3GNrmHxmz1lfRa24CjFObPXtjcetlOv5Ad299MhIK0znp3afw==
papaparse@^5.4.1:
version "5.4.1"
resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.4.1.tgz#f45c0f871853578bd3a30f92d96fdcfb6ebea127"
integrity sha512-HipMsgJkZu8br23pW15uvo6sib6wne/4woLZPlFf3rpDyMe9ywEXUsuD7+6K9PRkJlVT51j/sCOYDKGGS3ZJrw==

parse-glob@^3.0.4:
version "3.0.4"
Expand Down

0 comments on commit 1175c73

Please sign in to comment.