Class: Cocina::Models::Validators::DescriptionLocationVisitorValidator

Inherits:
BaseDescriptionVisitorValidator show all
Defined in:
lib/cocina/models/validators/description_location_visitor_validator.rb

Overview

Validates location.source.code against location_source_codes.yml and validates location.code against marc_country_codes.yml when source.code is marccountry.

Instance Method Summary collapse

Methods inherited from BaseDescriptionVisitorValidator

#path_to_s, #visit_array, #visit_obj

Instance Method Details

#validate!Object

Raises:



11
12
13
14
15
16
# File 'lib/cocina/models/validators/description_location_visitor_validator.rb', line 11

def validate!
  errors = []
  errors << "Unrecognized location source codes in description: #{error_paths.join(', ')}" if error_paths.any?
  errors << "Invalid MARC country codes in description: #{marc_country_error_paths.join(', ')}" if marc_country_error_paths.any?
  raise ValidationError, errors.join('; ') if errors.any?
end

#visit_hash(hash:, path:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cocina/models/validators/description_location_visitor_validator.rb', line 18

def visit_hash(hash:, path:)
  return unless location_path?(path)

  source_code = hash.dig(:source, :code)
  return unless source_code

  error_paths << "#{path_to_s(path)}.source.code (#{source_code})" unless valid_codes.include?(source_code.downcase)

  return unless source_code.downcase == 'marccountry'

  code = hash[:code]
  marc_country_error_paths << "#{path_to_s(path)}.code (#{code})" if code && !valid_marc_country_codes.include?(code.downcase)
end