A CSV Validator for Rails 3
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
spec Added test for presence of CSV file Jun 5, 2012
Gemfile Initial Commit Jun 5, 2012
Gemfile.lock Switched to ActionDispatch upload Jun 5, 2012
Rakefile Initial Commit Jun 5, 2012
csv_file_validator.gemspec gemspec typo fix Jun 5, 2012
csv_validator-0.0.3.gem New gem publish Jun 5, 2012



Add to your Gemfile:

gem 'csv_validator'


bundle install

Then add the following to your model:

attr_accessor :my_csv_file

validates :my_csv_file, :csv => true

This will check to see if it is a properly formed CSV file.

In this case, csv_validator expects :my_csv_file to be an instance of ActionDispatch::Http::UploadedFile, which is created by default for Rails uploads.

Other validation options

There are a handful of other options for more specific validation too. A complete list is below... note that you wouldn't use all simultaneously.

validates :my_csv_file, :csv => {
                          :columns => 3, # an exact number of columns
                          :max_columns => 10, # a maximum number of columns
                          :min_columns => 1, # a minimum number of columns
                          :rows => 20, # an exact number of rows
                          :max_rows => 30, # a maximum number of rows
                          :min_rows => 2, # a minimum number of rows
                          :email => 0, # will check to make sure all emails in specified column are valid email addresses
                          :numericality => 0, # will check to make sure all values in specified column are numerical