https://github.com/shystruk/csv-file-validator
🔧🔦 Validation of CSV file against user defined schema (returns back object with data and invalid messages)
https://github.com/shystruk/csv-file-validator
csv csv-files csv-parser csv-reader javascript validator
Last synced: 24 days ago
JSON representation
🔧🔦 Validation of CSV file against user defined schema (returns back object with data and invalid messages)
- Host: GitHub
- URL: https://github.com/shystruk/csv-file-validator
- Owner: shystruk
- License: mit
- Created: 2018-03-06T14:59:38.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-02-17T08:51:21.000Z (4 months ago)
- Last Synced: 2025-04-19T02:35:32.556Z (about 2 months ago)
- Topics: csv, csv-files, csv-parser, csv-reader, javascript, validator
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/csv-file-validator
- Size: 1.26 MB
- Stars: 94
- Watchers: 4
- Forks: 42
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# CSV File Validator [](https://twitter.com/intent/tweet?hashtags=javascript&original_referer=https%3A%2F%2Fpublish.twitter.com%2F&ref_src=twsrc%5Etfw&text=Validation%20of%20CSV%20file%20against%20user%20defined%20schema%20(returns%20back%20object%20with%20data%20and%20invalid%20messages)&tw_p=tweetbutton&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fcsv-file-validator&via=shystrukk) #
[](https://opensource.org/licenses/mit-license.php)
[](https://codecov.io/gh/shystruk/csv-file-validator)
[](https://travis-ci.org/shystruk/csv-file-validator)
[](https://snyk.io/test/github/shystruk/csv-file-validator?targetFile=package.json)
[](https://badge.fury.io/js/csv-file-validator)Validation of CSV file against user defined schema (returns back object with data and invalid messages)
## Getting csv-file-validator ##
#### npm
`npm install --save csv-file-validator`#### yarn
`yarn add csv-file-validator --save`## Example ##
```javascript
import CSVFileValidator from 'csv-file-validator'CSVFileValidator(file, config)
.then(csvData => {
csvData.data // Array of objects from file
csvData.inValidData // Array of error messages
})
.catch(err => {})
```Please see **Demo** for more details **/demo/index.html**

## API ##
### CSVFileValidator(file, config) ###
returns the Promise## file ##
Type: `File`.csv file
## config ##
Type: `Object`Config object should contain:
**headers** - Type: `Array`, row header (title) objects
**isHeaderNameOptional** - Type: `Boolean`, skip headers name if it is empty
**isColumnIndexAlphabetic** - Type: `Boolean`, convert numeric column index to alphabetic letter
**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)
isColumnIndexAlphabetic: false // default (optional)
}
```### name
Type: `String`
name of the row header (title)### inputName
Type: `String`
key name which will be return with value in a column### optional
Type: `Boolean`Makes column optional. If true column value will be return
### headerError
Type: `Function`If a header name is omitted or is not the same as in config *name* headerError function will be called with arguments
**headerValue, headerName, rowNumber, columnNumber**### required
Type: `Boolean`If required is true then a column value will be checked if it is not empty
### requiredError
Type: `Function`If value is empty requiredError function will be called with arguments
**headerName, rowNumber, columnNumber**### unique
Type: `Boolean`If it is true all header (title) column values will be checked for uniqueness
### uniqueError
Type: `Function`If one of the header value is not unique uniqueError function will be called with argument **headerName, rowNumber**
### validate
Type: `Function`Validate column value. As an argument column value will be passed
For e.g.
```javascript
/**
* @param {String} email
* @return {Boolean}
*/
function(email) {
return isEmailValid(email);
}
```### validateError
Type: `Function`If validate returns false validateError function will be called with arguments **headerName, rowNumber, columnNumber**
### dependentValidate
Type: `Function`Validate column value that depends on other values in other columns.
As an argument column value and row will be passed.
For e.g.
```javascript
/**
* @param {String} email
* @param {Array} row
* @return {Boolean}
*/
function(email, row) {
return isEmailDependsOnSomeDataInRow(email, row);
}
```### dependentValidateError
Type: `Function`If dependentValidate returns false dependentValidateError function will be called with arguments **headerName, rowNumber, columnNumber**
### isArray
Type: `Boolean`If column contains list of values separated by comma in return object it will be as an array
#### Config example ####
```javascript
const config = {
headers: [
{
name: 'First Name',
inputName: 'firstName',
required: true,
requiredError: function (headerName, rowNumber, columnNumber) {
return `${headerName} is required in the ${rowNumber} row / ${columnNumber} column`
}
},
{
name: 'Last Name',
inputName: 'lastName',
required: false
},
{
name: 'Email',
inputName: 'email',
unique: true,
uniqueError: function (headerName) {
return `${headerName} is not unique`
},
validate: function(email) {
return isEmailValid(email)
},
validateError: function (headerName, rowNumber, columnNumber) {
return `${headerName} is not valid in the ${rowNumber} row / ${columnNumber} column`
}
},
{
name: 'Roles',
inputName: 'roles',
isArray: true
},
{
name: 'Country',
inputName: 'country',
optional: true,
dependentValidate: function(email, row) {
return isEmailDependsOnSomeDataInRow(email, row);
}
}
]
}
```## Contributing
Any contributions you make **are greatly appreciated**.
Please read the [Contributions Guidelines](CONTRIBUTING.md) before submitting a PR.
## License
MIT © [Vasyl Stokolosa](https://about.me/shystruk)