mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

strict: no documentation about row length when strict is enabled #190

Closed vicradon closed 3 years ago

vicradon commented 3 years ago

Documentation Is:

Please Explain in Detail...

When strict is set to true and the headers supplied are less than the number of headers in the CSV, this error is thrown

RangeError: Row length does not match headers
    at CsvParser.parseLine (/home/runner/TakeHome/node_modules/csv-parser/index.js:163:17)
    at CsvParser._transform (/home/runner/TakeHome/node_modules/csv-parser/index.js:252:16)

For example, given this CSV

First name,Last name
Mocha,Chai
React,Redux

Supplying this data

    const results = await neatCsv(csvData, { separator: ",", skipLines: 1, strict: true, headers: ["First name"]  })

throws the error.

Your Proposal for Changes

Some more documentation on what should be included when strict is set to true.

TrySound commented 3 years ago

You pass only one header in options. This leads to the error.

vicradon commented 3 years ago

@TrySound Must all available headers be passed?

TrySound commented 3 years ago

csv-parser uses either provided headers or passed via options. Strict flag in both cases requires headers count to match columns in each row. From your example it looks like the first row is already a header so you can just remove headers option.

vicradon commented 3 years ago

Alright, thanks