Module: Bulkrax::CsvRow::ChildReference

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

Overview

Validates that any child references in a row point to source identifiers that exist either elsewhere in the same CSV or as existing repository records. Uses context (Set of all source identifiers) to validate references within the CSV, and context (callable) to look up existing records in the same way the importer does at runtime. Skips validation when all_ids is empty and fill_in_blank_source_identifiers is configured, since generated identifiers cannot be cross-referenced at validation time.

Class Method Summary collapse

Class Method Details

.call(record, row_index, context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/validators/bulkrax/csv_row/child_reference.rb', line 14

def self.call(record, row_index, context)
  all_ids = context[:all_ids]
  return if all_ids.empty? && Bulkrax.fill_in_blank_source_identifiers.present?

  find_record = context[:find_record_by_source_identifier]

  collect_child_ids(record, context).each do |child_id|
    next if all_ids.include?(child_id)
    next if find_record&.call(child_id)

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