Class: DocumentDataDictionary::CsvHeaderValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- DocumentDataDictionary::CsvHeaderValidator
- Defined in:
- app/models/document_data_dictionary/csv_header_validator.rb
Overview
CsvHeaderValidator
Instance Method Summary collapse
Instance Method Details
#valid_csv_headers?(csv_file) ⇒ Boolean
23 24 25 26 27 28 |
# File 'app/models/document_data_dictionary/csv_header_validator.rb', line 23 def valid_csv_headers?(csv_file) headers = CSV.parse(csv_file.download)[0] (["friendlier_id", "field_name", "field_type", "values", "definition", "definition_source", "parent_field_name"] - headers).empty? rescue ArgumentError, ActiveStorage::FileNotFoundError false end |
#validate(record) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/models/document_data_dictionary/csv_header_validator.rb', line 9 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. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.") # Log the CSV file content Rails.logger.error("CSV validation failed. CSV content: #{record.csv_file.download}") end valid_csv_header end |