tilo/smarter_csv

[Question] Original names of missing Headers

Piioo opened this issue · 6 comments

Piioo commented

Hello,

how do I get the original names of the missing header columns?

SmarterCSV::MissingKeys contains a message but not the keys and not the original keys.

tilo commented

🤔 the missing keys should be added to the message..

raise SmarterCSV::MissingKeys, "ERROR: missing attributes: #{missing_keys.join(',')}"

@Piioo Can you please provide a simple CSV file that reproduces the issue, along with the options you're using?

Piioo commented

We have a csv with columns Foo;Bar;Baz and we do

SmarterCSV.process(file, keep_original_headers: true, silence_missing_keys: true, key_mapping: { "Foo" => :one, "Bar" => :two, "Baz" => :three }, required_keys: { :one, :two, :three }

When the customer write now the Baz column wrong in the csv (Baz2) we get:
#<SmarterCSV::MissingKeys: ERROR: missing attributes: three>

How do I get the original name Baz2 to return this to the customer?

(I know, I may try to parse the message and and then check my key_mapping, but this is not a good solution)

I want to let the customer know which header is wrong in his file (original name)

tilo commented

@Piioo thank you for explaining!

this is a bit tricky, because the required_keys are what is expected after mapping of headers. The code just knows that you expect :three and raises the error that it is missing.
This part of the code does not understand what the original CSV-header was supposed to be named, or what other (unexpected) column was in the CSV-file.

For debugging you can get to the original headers from the CSV-file via SmarterCSV.headers.

Does that work for you?

tilo commented

I added this to the error message:

raise SmarterCSV::MissingKeys, "ERROR: missing attributes: #{missing_keys.join(',')}. Check `SmarterCSV.headers` for original headers."

tilo commented

@Piioo version 1.10.2 has the improved error message

#272

Piioo commented

Thanks you very much :)