csv-validator
nodejs validator for csv file
Features
- Parse csv (supports only file as of now) and convert to json array
- Validate each line with type or RegExp
- Return each line's error with error message
- Support callback and promise
- Non-blocking parsing
Install
npm install csv-validator --save
Example
Without errors:
const csv = require('csv-validator');
const csvFilePath = '<path to csv file>';
const headers = {
name: '',
phone: 1,
email: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
_country: ''
};
csv(csvFilePath, headers)
.then(console.log)
With error:
const csv = require('csv-validator');
const csvFilePath = '<path to csv file>';
const headers = {
name: '',
phone: 1,
email: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
country: ''
};
csv(csvFilePath, headers)
.then(console.log)
.catch(console.error)
Error messages:
- ... must be a type number: the column should be a number
- ... is required: the column should be filled
- ... must be in /^[0-9]?.*/: the column should be in RegExp (in this example: /^[0-9]?$/)
Contribution
- Any pull request for new features and bug fixes is appreciated.
- Just make sure to add test.
LICENSE
MIT