Module: Bulkrax::CsvRow::ControlledVocabulary

Defined in:
app/validators/bulkrax/csv_row/controlled_vocabulary.rb

Overview

Validates that controlled vocabulary values in each row are valid according to the QA authority for that field.

Class Method Summary collapse

Class Method Details

.call(record, row_index, context) ⇒ Object

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/validators/bulkrax/csv_row/controlled_vocabulary.rb', line 9

def self.call(record, row_index, context) # rubocop:disable Metrics/MethodLength
   = context[:field_metadata]
  return if .blank?

  model = record[:model]
   = [model]
  return if .blank?

  controlled_terms = [:controlled_vocab_terms] || []
  return if controlled_terms.blank?

  controlled_terms.each do |field|
    value = record[:raw_row][field]
    next if value.blank?

    authority = load_authority(field)
    next if authority.nil?

    term = authority.find(value)
    next unless term.blank? || term.dig('active') == false

    context[:errors] << {
      row: row_index,
      source_identifier: record[:source_identifier],
      severity: 'error',
      category: 'invalid_controlled_value',
      column: field,
      value: value,
      message: I18n.t('bulkrax.importer.guided_import.validation.controlled_vocabulary_validator.errors.message',
                      value: value, field: field),
      suggestion: suggestion(value, authority)
    }
  end
end