Sha256: ae8a1e8260b9fb9f06299753acb5ba8a5ab4c1bf301fdf312c443f732f0744ec

Contents?: true

Size: 729 Bytes

Versions: 2

Compression:

Stored size: 729 Bytes

Contents

# frozen_string_literal: true

require "csv"

# CSV Header Validation
class Import
  # CsvHeaderValidator
  class CsvHeaderValidator < ActiveModel::Validator
    def validate(record)
      valid_csv_header = true
      unless valid_csv_headers?(record&.csv_file)
        valid_csv_header = false
        record.errors.add(:csv_file,
          "Missing a required CSV header. Title, Resource Class, Access Rights, and ID are required.")
      end

      valid_csv_header
    end

    def valid_csv_headers?(csv_file)
      headers = CSV.parse(csv_file.download)[0]
      (["Title", "Resource Class", "Access Rights", "ID"] - headers).empty?
    rescue ArgumentError, ActiveStorage::FileNotFoundError
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geoblacklight_admin-0.6.0 app/models/import/csv_header_validator.rb
geoblacklight_admin-0.5.1 app/models/import/csv_header_validator.rb