Class: Hyrax::ControlledVocabularyValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/hyrax/controlled_vocabulary_validator.rb

Overview

Validates that controlled vocabulary properties contain only active terms from their corresponding local QA authority.

Properties are matched to authorities dynamically via Qa::Authorities::Local.subauthorities, using the singularized authority name to match property names on the change set.

Only covers local authorities (file-based and table-based). Remote authorities (e.g. Geonames) are out of scope.

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/validators/hyrax/controlled_vocabulary_validator.rb', line 14

def validate(record)
  return unless Flipflop.validate_local_controlled_vocabulary?

  active_terms_by_property(record).each do |property, terms|
    values = Array.wrap(record.public_send(property)).reject(&:blank?)
    next if values.empty?

    invalid = values.reject { |v| terms.include?(v) }
    invalid.each do |v|
      record.errors.add(property, "#{property.to_s.humanize} contains unrecognized value: #{v}")
    end
  end
end