Sha256: 6bdf7f346dbb6e1bed5be7567e0032b1c9780616f2d0974bd5fda7549fcb8edf
Contents?: true
Size: 905 Bytes
Versions: 1
Compression:
Stored size: 905 Bytes
Contents
require 'active_model' require 'csv' require 'nkf' module ActiveModel module Validations class CsvValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) content = NKF.nkf('-w', File.read(value.path, universal_newline: true)) rows = CSV.parse(content, headers: :first_row, converters: :integer) if options[:max].present? && rows.size > options[:max] record.errors.add(attribute, :max_rows, options.slice(:max).merge(rows: rows.size)) end if options[:headers].present? missing_headers = options[:headers] - rows.headers if missing_headers.present? record.errors.add(attribute, :missing_headers, missing_headers: missing_headers.to_sentence) end end rescue CSV::MalformedCSVError record.errors.add(attribute, :invalid_csv) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activemodel-csv_validator-0.1.0 | lib/activemodel/validations/csv_validator.rb |