Class: QcResultsUploadValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- QcResultsUploadValidator
- Defined in:
- app/validators/qc_results_upload_validator.rb
Overview
Failed validations return unprocessable_entity These are being validated before any QC entity is created
Instance Method Summary collapse
- #validate(record) ⇒ Object
- #validate_csv_data(record) ⇒ Object
- #validate_fields(record) ⇒ Object
- #validate_headers(record) ⇒ Object
- #validate_rows(record) ⇒ Object
- #validate_used_by(record) ⇒ Object
Instance Method Details
#validate(record) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'app/validators/qc_results_upload_validator.rb', line 6 def validate(record) validations = %i[validate_used_by validate_csv_data validate_rows validate_headers validate_fields] validations.each do |validation| next if record.errors.present? send(validation, record) end end |
#validate_csv_data(record) ⇒ Object
25 26 27 28 29 30 |
# File 'app/validators/qc_results_upload_validator.rb', line 25 def validate_csv_data(record) return if record.csv_data.present? record.errors.add :csv_data, 'Is missing' nil end |
#validate_fields(record) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'app/validators/qc_results_upload_validator.rb', line 58 def validate_fields(record) missing_headers = ([:required_headers] - @headers) return if missing_headers.empty? record.errors.add :csv_data, "Missing required headers: #{missing_headers.join(',')}" nil end |
#validate_headers(record) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/validators/qc_results_upload_validator.rb', line 46 def validate_headers(record) # Remove whitespace and empty headers @headers = @header_row.split(',').map(&:strip).compact_blank # Case sensitive # e.g. "Genome Size" != "Genome size" return if @headers.count == @headers.uniq.count record.errors.add :csv_data, 'Contains duplicated headers' nil end |
#validate_rows(record) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/validators/qc_results_upload_validator.rb', line 32 def validate_rows(record) @header_row = record.csv_data.split("\n")[1] unless @header_row record.errors.add :csv_data, 'Missing header row' return end data_rows = record.csv_data.split("\n")[2..] return if data_rows.present? record.errors.add :csv_data, 'Missing data rows' nil end |
#validate_used_by(record) ⇒ Object
17 18 19 20 21 22 23 |
# File 'app/validators/qc_results_upload_validator.rb', line 17 def validate_used_by(record) qc_assay_types = QcAssayType.where(used_by: record.used_by) return unless qc_assay_types.empty? record.errors.add :used_by, 'No QcAssayTypes belong to used_by value' nil end |