1.1.0 โ€ข Published 5 months ago

@saurabhgkp/csv-checker v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

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: object

    • headers: 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 install

Test locally:

node index.js

๐Ÿ“„ License

MIT ยฉ 2025 Saurabh

1.1.0

5 months ago

1.0.0

5 months ago