true-csv
v1.2.3
Published
Validate CSV headers and data row-by-row
Readme
true-csv
🔍 A lightweight utility to validate CSV string content with custom rules for headers and fields.
📦 Installation
npm install true-csv🚀 Usage
import { validateCSV } from 'true-csv';
const csv = `
Name,Email,Age
Saurabh,[email protected],25
John,john[at]email,16
Saurabh,[email protected],25
Saurabh,[email protected],25
saur,[email protected],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": "[email protected]", "Age": "25" },
{ "Name": "saur", "Email": "[email protected]", "Age": "20" }
],
"invalidRows": [
{
"row": 3,
"data": { "Name": "John", "Email": "john[at]email", "Age": "16" }
},
{
"row": 4,
"data": { "Name": "Saurabh", "Email": "[email protected]", "Age": "25" }
},
{
"row": 5,
"data": { "Name": "Saurabh", "Email": "[email protected]", "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
