Module: CompanyNumber::Validation

Defined in:
lib/company_number/validation.rb

Constant Summary collapse

ISO_CODE_REGEXP =
/^[A-Za-z]{2}$/.freeze
AVAILABLE_METADATA_KEYS =
%i[
  variations
  pattern
  country
  regexp
  name
].freeze

Class Method Summary collapse

Class Method Details

.check_country_code_metadata(metadata) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/company_number/validation.rb', line 43

def ()
  .each do |key, value|
    check_object_inclusion(key, AVAILABLE_METADATA_KEYS)
    object_class = key == :variations ? Array : String
    check_object_class(value, [object_class])
  end
end

.check_dictionary_structure(value) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/company_number/validation.rb', line 51

def check_dictionary_structure(value)
  Validation.check_object_class(value, [Hash])

  value.each do |country_code, |
    Validation.check_object_class(country_code, [Symbol])
    Validation.check_object_class(, [Hash])
  end
end

.check_iso_code_format(country_code) ⇒ Object



36
37
38
39
40
41
# File 'lib/company_number/validation.rb', line 36

def check_iso_code_format(country_code)
  return unless country_code

  check_object_class(country_code, [Symbol, String])
  check_string_format(country_code.to_s, ISO_CODE_REGEXP)
end

.check_object_class(object, expected_classes = []) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/company_number/validation.rb', line 16

def check_object_class(object, expected_classes = [])
  return if expected_classes.include?(object.class)

  raise ArgumentError,
        "Expect #{object} class to be #{expected_classes.join(', ')}"
end

.check_object_inclusion(object, expected_objects = []) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/company_number/validation.rb', line 23

def check_object_inclusion(object, expected_objects = [])
  return if expected_objects.include?(object)

  raise ArgumentError,
        "Expect #{object} to be part of #{expected_objects}"
end

.check_string_format(string, regexp) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/company_number/validation.rb', line 30

def check_string_format(string, regexp)
  return if string =~ regexp

  raise ArgumentError, "Expect #{string} to match regexp: #{regexp}"
end