class Validator
Example VALIDATIONS
object
VALIDATIONS = { validation_tests: { "column_name": { functions: [:validation_method_1] } options: [{label: 'English', value: 'en'}, {label: 'German', 'gem'}] } }
Constants
- VALIDATIONS
Public Instance Methods
always_false(value)
click to toggle source
# File lib/csv_content_validator.rb, line 108 def always_false value false end
always_true(value)
click to toggle source
General Validation Methods
# File lib/csv_content_validator.rb, line 104 def always_true value true end
inspect()
click to toggle source
# File lib/csv_content_validator.rb, line 95 def inspect validations.inspect end
is_valid?(column_name, value)
click to toggle source
# 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=[])
click to toggle source
# 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()
click to toggle source
# File lib/csv_content_validator.rb, line 99 def to_json validations.to_json; end
validate_fatal_errors(csv_file)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
validations()
click to toggle source
# File lib/csv_content_validator.rb, line 114 def validations @validations ||= self.class::VALIDATIONS end