Class: Validator
- Inherits:
-
Object
- Object
- Validator
- Defined in:
- lib/csv_content_validator.rb
Overview
Example VALIDATIONS object
VALIDATIONS =
{
validation_tests: {
"column_name": {
functions: [:validation_method_1] }
options: [{label: 'English', value: 'en'}, {label: 'German', 'gem'}]
}
}
Constant Summary collapse
- VALIDATIONS =
{ validation_tests: { } }
Instance Method Summary collapse
- #always_false(value) ⇒ Object
-
#always_true(value) ⇒ Object
General Validation Methods.
- #inspect ⇒ Object
- #is_valid?(column_name, value) ⇒ Boolean
- #render_errors(csv_file, corrections = []) ⇒ Object
- #to_json ⇒ Object
- #validate_fatal_errors(csv_file) ⇒ Object
- #validate_file(csv_file, corrections) ⇒ Object
- #validate_file!(csv_file, corrections) ⇒ Object
Instance Method Details
#always_false(value) ⇒ Object
108 109 110 |
# File 'lib/csv_content_validator.rb', line 108 def always_false value false end |
#always_true(value) ⇒ Object
General Validation Methods
104 105 106 |
# File 'lib/csv_content_validator.rb', line 104 def always_true value true end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/csv_content_validator.rb', line 95 def inspect validations.inspect end |
#is_valid?(column_name, value) ⇒ Boolean
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/csv_content_validator.rb', line 78 def is_valid? (column_name, value) return true unless validations[:validation_tests].has_key?(column_name.to_sym) validation_tests = validations[:validation_tests][column_name.to_sym] # call all methods if validation_tests[:functions] then validation_tests[:functions].each do |function| return false unless send(function, value) end elsif validation_tests[:options] then return true if value === "" && !validation_tests[:required] return validation_tests[:options].any?{|option| option[:value] === value} end return true end |
#render_errors(csv_file, corrections = []) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/csv_content_validator.rb', line 30 def render_errors (csv_file, corrections=[]) begin rows = validate_file(csv_file, corrections) if rows[:errors] then return {json: {errors:rows[:errors]}, :status => 420} else return {json: {validator: to_json, rows: rows[:rows]}} end rescue puts "rescued!" return {json: {errors: ["CSV file was malformed"]}, :status => 420 } end end |
#to_json ⇒ Object
99 100 101 |
# File 'lib/csv_content_validator.rb', line 99 def to_json validations.to_json; end |
#validate_fatal_errors(csv_file) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/csv_content_validator.rb', line 20 def validate_fatal_errors(csv_file) fatal_errors = [] validations[:validation_tests].keys.each do |required_column| unless csv_file.headers.include?(required_column.to_s) then fatal_errors.push("Uploaded CSV missing required column '#{required_column}'") end end return fatal_errors end |
#validate_file(csv_file, corrections) ⇒ Object
74 75 76 |
# File 'lib/csv_content_validator.rb', line 74 def validate_file(csv_file, corrections) return validate_file!(csv_file, corrections) end |
#validate_file!(csv_file, corrections) ⇒ Object
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 |
# File 'lib/csv_content_validator.rb', line 44 def validate_file! (csv_file, corrections) csv_rows = CSV.read(csv_file, headers:true) # check for fatal errors fatal_errors = validate_fatal_errors(csv_rows) return {errors:fatal_errors} unless fatal_errors.empty? rows = [] # no fatal errors - validate cells accordingly curr_correction_index = 0 CSV.foreach(csv_file, headers:true) do |row| o = {} if corrections[curr_correction_index] && $. === corrections[curr_correction_index]["number"] then o = corrections[curr_correction_index]["row"] curr_correction_index += 1 else o = row.to_h end is_valid = true o.keys.each do |column| is_valid = is_valid && is_valid?(column, o[column]) end unless is_valid then rows << {number:$., row:o} end end return {rows:rows} end |