Skip to content

Commit

Permalink
Merge pull request #112 from shystruk/dev
Browse files Browse the repository at this point in the history
2.1.0. Convert column to number. Support dynamicTyping. Header validation.
  • Loading branch information
shystruk authored Jan 26, 2023
2 parents fb5557d + d4fc999 commit 0f6c355
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 86 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ Type: `Object` <br>

Config object should contain:<br>
**headers** - Type: `Array`, row header (title) objects<br>
**isHeaderNameOptional** - Type: `Boolean`, skip the header name if it is empty<br>
**isHeaderNameOptional** - Type: `Boolean`, skip headers name if it is empty<br>
**isColumnIndexAlphabetic** - Type: `Boolean`, convert numeric column index to alphabetic letter<br>
**parserConfig** - Type: `Object`, optional, [papaparse](https://www.papaparse.com/docs#config) options.
Default options, which can't be overridden: **skipEmptyLines**, **complete** and **error**

```javascript
const config = {
headers: [], // required
isHeaderNameOptional: false // default (optional)
isHeaderNameOptional: false, // default (optional)
isColumnIndexAlphabetic: false // default (optional)
}
```

Expand All @@ -78,7 +80,7 @@ If a header name is omitted or is not the same as in config *name* headerError f
### required
Type: `Boolean` <br>

If required is true than a column value will be checked if it is not empty
If required is true then a column value will be checked if it is not empty

### requiredError
Type: `Function` <br>
Expand Down
5 changes: 5 additions & 0 deletions demo-ts/demo_comma.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name,Surname,Age
Test,Test,1
Test_1,,2
Test_2,Test_2,
,Test_3,1
216 changes: 183 additions & 33 deletions demo-ts/dist/bundle.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions demo-ts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
</style>
</head>
<body>
<input type="file" accept=".csv" id="file" />
<div>Semicolon delimiter (demo.csv)</div>
<p><input type="file" accept=".csv" id="file" /></p>
<div id="invalidMessages"></div>

<div id="invalidMessages"></div>
<br/><br/>

<div>Comma delimiter (demo_comma.csv)</div>
<p><input type="file" accept=".csv" id="file_1" /></p>
<div id="invalidMessages_1"></div>

<script src="dist/bundle.js"></script>
</body>
</html>
</html>
35 changes: 34 additions & 1 deletion demo-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const isEmailValid = function (email: string) {
return reqExp.test(email)
}

const isAgeValid = function (age:number) {
return age > 0
}

const isPasswordValid = function (password: string) {
return password.length >= 4
}
Expand All @@ -29,14 +33,21 @@ interface CSVRow {
roles: string;
}

interface CSVRow_1 {
name: string;
surname: string;
age: string;
}

const CSVConfig: ValidatorConfig = {
headers: [
{ name: 'First Name', inputName: 'firstName', 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 }
]
],
isColumnIndexAlphabetic: true
}

document.getElementById('file').onchange = function (event: any) {
Expand All @@ -49,3 +60,25 @@ document.getElementById('file').onchange = function (event: any) {
console.log(csvData.data)
})
}

const CSVConfig_1: ValidatorConfig = {
headers: [
{ name: 'Name', inputName: 'name', required: true, requiredError },
{ name: 'Surname', inputName: 'surname', required: true, requiredError, optional: true },
{ name: 'Age', inputName: 'age', required: true, requiredError, validate: isAgeValid, validateError },
],
parserConfig: {
dynamicTyping: true
}
}

document.getElementById('file_1').onchange = function (event: any) {
CSVFileValidator<CSVRow_1>(event.target.files[0], CSVConfig_1)
.then((csvData: ParsedResults) => {
csvData.inValidData.forEach((item) => {
document.getElementById('invalidMessages_1').insertAdjacentHTML('beforeend', item.message)
})
console.log(csvData.inValidData)
console.log(csvData.data)
})
}
5 changes: 5 additions & 0 deletions demo/demo_comma.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name,Surname,Age
Test,Test,0
Test_1,,2
Test_2,Test_2,
,Test_3,1
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.

12 changes: 9 additions & 3 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
</style>
</head>
<body>
<input type="file" accept=".csv" id="file" />
<div>Semicolon delimiter (demo.csv)</div>
<p><input type="file" accept=".csv" id="file" /></p>
<div id="invalidMessages"></div>

<div id="invalidMessages"></div>
<br/><br/>

<div>Comma delimiter (demo_comma.csv)</div>
<p><input type="file" accept=".csv" id="file_1" /></p>
<div id="invalidMessages_1"></div>

<script src="dist/bundle.js"></script>
</body>
</html>
</html>
29 changes: 28 additions & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const isEmailValid = function (email) {
return reqExp.test(email)
}

const isAgeValid = function (age) {
return age > 0
}

const isPasswordValid = function (password) {
return password.length >= 4
}
Expand All @@ -28,7 +32,8 @@ 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 }
]
],
isColumnIndexAlphabetic: true
}

document.getElementById('file').onchange = function (event) {
Expand All @@ -41,3 +46,25 @@ document.getElementById('file').onchange = function (event) {
console.log(csvData.data)
})
}

const CSVConfig_1 = {
headers: [
{ name: 'Name', inputName: 'name', required: true, requiredError },
{ name: 'Surname', inputName: 'surname', required: true, requiredError, optional: true },
{ name: 'Age', inputName: 'age', required: true, requiredError, validate: isAgeValid, validateError },
],
parserConfig: {
dynamicTyping: true
}
}

document.getElementById('file_1').onchange = function (event) {
CSVFileValidator(event.target.files[0], CSVConfig_1)
.then(csvData => {
csvData.inValidData.forEach(item => {
document.getElementById('invalidMessages_1').insertAdjacentHTML('beforeend', item.message)
})
console.log(csvData.inValidData)
console.log(csvData.data)
})
}
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": "2.0.0",
"version": "2.1.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
5 changes: 3 additions & 2 deletions src/csv-file-validator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface FieldSchema {
/** Makes column optional. If true column value will be return */
optional?: boolean;

/** If required is true than a column value will be checked if it is not empty */
/** If required is true then a column value will be checked if it is not empty */
required?: boolean;

/** If it is true all header (title) column values will be checked for uniqueness */
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface FieldSchema {
* Validate column value.
* Must return true for valid field and false for invalid.
*/
validate?: (field: string) => boolean;
validate?: (field: string|number|boolean) => boolean;

/**
* Validate column value that depends on other values in other columns.
Expand Down Expand Up @@ -83,6 +83,7 @@ export interface ValidatorConfig {
headers: FieldSchema[];
isHeaderNameOptional?: boolean;
parserConfig?: ParseConfig;
isColumnIndexAlphabetic?: boolean;
}

export default function CSVFileValidator<Row = any, Error = RowError>(
Expand Down
Loading

0 comments on commit 0f6c355

Please sign in to comment.