1.1.0 โข Published 5 months ago
@saurabhgkp/csv-checker v1.1.0
csv-checker
๐ A lightweight utility to validate CSV string content with custom rules for headers and fields.
๐ฆ Installation
npm install @saurabhgkp/csv-checker๐ Usage
import { validateCSV } from '@saurabhgkp/csv-checker';
const csv = `
Name,Email,Age
Saurabh,saurabh@mail.com,25
John,john[at]email,16
Saurabh,saurabh@mail.com,25
Saurabh,saurabh@mail.com,25
saur,missing@domain.com,20
`;
const result = validateCSV(csv, {
headers: ["Name", "Email", "Age"],
rules: {
Name: { required: true },
Email: { required: true, pattern: "^\\S+@\\S+\\.\\S+$" },
Age: { required: true, type: "number", min: 18 }
}
});
console.log(JSON.stringify(result, null, 2));โ API
validateCSV(csvString, options)
csvString:
string
Raw CSV content as a string.options:
objectheaders:string[]โ List of expected headers (column names).rules:objectโ Validation rules for each field.
๐ง Rule Format
Each rule object can have the following:
required:booleanโ Whether the field is required.pattern:stringโ A regex pattern the field must match.type:'number'โ Enforce that the field is a number.min:numberโ Minimum value (if type is number).
๐ค Output Structure
{
headerValid: true | false,
errors: [
{ row: 0, column: 'Header', message: '...' }
],
validRows: [ { Name: '', Email: '', Age: '' }, ... ],
invalidRows: [ { row: 3, data: { ... } }, ... ]
}๐งช Example Output
{
"headerValid": true,
"errors": [],
"validRows": [
{ "Name": "Saurabh", "Email": "saurabh@mail.com", "Age": "25" },
{ "Name": "saur", "Email": "missing@domain.com", "Age": "20" }
],
"invalidRows": [
{
"row": 3,
"data": { "Name": "John", "Email": "john[at]email", "Age": "16" }
},
{
"row": 4,
"data": { "Name": "Saurabh", "Email": "saurabh@mail.com", "Age": "25" }
},
{
"row": 5,
"data": { "Name": "Saurabh", "Email": "saurabh@mail.com", "Age": "25" }
}
]
}๐ Dev
Clone locally:
git clone https://github.com/your-username/csv-checker.git
cd csv-checker
npm installTest locally:
node index.js๐ License
MIT ยฉ 2025 Saurabh