Class: CsvValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/csv_validator.rb

Constant Summary collapse

@@default_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



9
10
11
# File 'lib/csv_validator.rb', line 9

def self.default_options
  @@default_options
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/csv_validator.rb', line 13

def validate_each(record, attribute, value)
  options = @@default_options.merge(self.options)
  
  unless value
    record.errors.add(attribute, options[:message] || "must be present")
    return
  end
  
  begin
    csv = CSV.read(value.path)
  rescue CSV::MalformedCSVError
    record.errors.add(attribute, options[:message] || "is not a valid CSV file")
    return
  end
  
  if options[:columns]
    unless csv[0].length == options[:columns]
      record.errors.add(attribute, options[:message] || "should have #{options[:columns]} columns")
    end
  end

  if options[:max_columns]
    if csv[0].length > options[:max_columns]
      record.errors.add(attribute, options[:message] || "should have no more than #{options[:max_columns]} columns")
    end
  end

  if options[:min_columns]
    if csv[0].length < options[:min_columns]
      record.errors.add(attribute, options[:message] || "should have at least #{options[:min_columns]} columns")
    end
  end

  if options[:rows]
    unless csv.length == options[:rows]
      record.errors.add(attribute, options[:message] || "should have #{options[:rows]} rows")
    end
  end

  if options[:min_rows]
    if csv.length < options[:min_rows]
      record.errors.add(attribute, options[:message] || "should have at least #{options[:min_rows]} rows")
    end
  end

  if options[:max_rows]
    if csv.length > options[:max_rows]
      record.errors.add(attribute, options[:message] || "should have no more than #{options[:max_rows]} rows")
    end
  end

  if options[:email]
    emails = column_to_array(csv, options[:email])
    invalid_emails = []
    emails.each do |email|
      begin
        address = Mail::Address.new email
        valid = address.address == email && address.domain
        tree = address.__send__(:tree)        
        valid &&= (tree.domain.dot_atom_text.elements.size > 1)
      rescue
        valid = false
      end
      unless valid
        invalid_emails << email
      end
    end
    
    if invalid_emails.length > 0
      record.errors.add(attribute, options[:message] || "contains invalid emails (#{invalid_emails.join(', ')})")
    end
  end

  if options[:numericality]
    numbers = column_to_array(csv, options[:numericality])
    numbers.each do |number|
      unless is_numeric?(number)
        record.errors.add(attribute, options[:message] || "contains non-numeric content in column #{options[:numericality]}")
        return
      end
    end
  end
  
end