beamworks / react-csv-importer

Uploader + CSV parser + raw file preview + UI for custom user column mapping, all in one
https://www.npmjs.com/package/react-csv-importer
MIT License
231 stars 95 forks source link

Detect missing required field in a row #37

Closed kellykampen closed 3 years ago

kellykampen commented 3 years ago

Hi,

Thanks again for this package.

Is there a way to easily detect a required field that is missing from a row?

Example: Name & Email are required:

Name Email Age
Joe joe@example.com 22
Jane

The importer only checks if the headers are present, and allows Jane row to go through.

I am using processChunk, but thought this might be a nice feature of the package if it was built-in since the importer already knows which fields are required vs optional.

Sidenote: It would be awesome if processChunk also returned the row number. This way I could tell the user which row has an issue.

Any thoughts? Thanks in advance.

unframework commented 3 years ago

Hi Kelly, happy that the library is useful!

Validating data is definitely a meaty topic, and as you know other libraries in this niche do offer that as a feature. One of my main design assumptions was the support for data streaming - i.e. the code does not "slurp" the entire file until the user hits "Import". (In fact, PapaParse does not do that client-side out of the box and I had to do some kludging to make it work in the browser.)

Due to that assumption the library can only do the validation around the same time as when processChunk runs, i.e. when it is too late to warn the user/etc. So basically by definition there isn't really much that can be done prior to processing start - although maybe there could be some kind of "preview validation" step? There is always X kilobytes being read to show column preview/etc, so maybe that could be enough to do a sanity check on the user's data?

Now, as for validation during main processing phrase - as is, some extended UI can already be shown while processing is happening. It is possible to pause parsing on erroneous rows (the approach is to have processChunk return a promise that only resolves when user e.g. performs some action). Maybe the library could allow showing the UI inside the progress box... But that touches on the more general topic of customization/theming that I am planning to tackle next. Would really appreciate thoughts on what would be most useful!

Finally, re: returning row number to processChunk is a great idea! I will add that to the to-do pile, should be a couple days to get it published. Thinking of reporting it as e.g. rowsCompleted count or something like that.

Thanks for the feedback, and would love to hear more thoughts on whether e.g. preview validation would be useful or maybe assisting with some mid-processing UI display?

unframework commented 3 years ago

Added an info object with startIndex field as second param to the processChunk - please try it in v0.5.0.

unframework commented 3 years ago

Closing for now, thanks again for reporting!

bryanjtc commented 1 year ago

Is there an example with rowValidation using this info object?