-
-
Notifications
You must be signed in to change notification settings - Fork 190
Header Validations
The main reason for :header_validations
is for you to be able to abort processing the CSV file if the headers are not correct. e.g. if someone uploads an incorrect file to your application.
You probably don't want to disable this validation.
It checks what the column headers of your CSV file look after transformation, and throws SmarterCSV::DuplicateHeaders
if there are duplicates.
employee_id,email,first_name,last_name,email,department
Which of the columns labeled email
would you like to use for the values in your data hashes?
For an application this is a no-go, and that's why an exception is thrown.
Note: If you manually process such a file, the :user_provided_headers
option comes in handy.
Let's say someone uploads this kind of CSV file:
employee_id,email,first_name,last_name,department
But you really need the manager_email
column for your application.
options = {
header_validations: [ required_headers: [:email,:manager_email] ]
}
In previous versions (only tested against v1.2.6) this is passed in like this:
options = {
required_headers: [:email, :manager_email]
}
If a CSV file does not contain both of these headers after transformation, the validation will throw SmarterCSV::MissingHeaders
.
The invalid_headers_spec.rb
has some examples on how to use this.
As with the header_transformations, you can also define custom header_validations through Procs.
You can always define your own custom Procs to do header validations.
Check out the sample code in header_validations.rb