Class: SimpleFormsApiSubmission::MetadataValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_forms_api_submission/metadata_validator.rb

Class Method Summary collapse

Class Method Details

.validate(metadata, zip_code_is_us_based: true) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 5

def self.validate(, zip_code_is_us_based: true)
  validate_first_name()
    .then { |m| validate_last_name(m) }
    .then { |m| validate_file_number(m) }
    .then { |m| validate_zip_code(m, zip_code_is_us_based) }
    .then { |m| validate_source(m) }
    .then { |m| validate_doc_type(m) }
end

.validate_doc_type(metadata) ⇒ Object



61
62
63
64
65
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 61

def self.validate_doc_type()
  validate_presence_and_stringiness(['docType'], 'doc type')

  
end

.validate_file_number(metadata) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 30

def self.validate_file_number()
  validate_presence_and_stringiness(['fileNumber'], 'file number')
  unless ['fileNumber'].match?(/^\d{8,9}$/)
    raise ArgumentError, 'file number is invalid. It must be 8 or 9 digits'
  end

  
end

.validate_first_name(metadata) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 14

def self.validate_first_name()
  validate_presence_and_stringiness(['veteranFirstName'], 'veteran first name')
  ['veteranFirstName'] =
    I18n.transliterate(['veteranFirstName']).gsub(%r{[^a-zA-Z\-\/\s]}, '').strip.first(50)

  
end

.validate_last_name(metadata) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 22

def self.validate_last_name()
  validate_presence_and_stringiness(['veteranLastName'], 'veteran last name')
  ['veteranLastName'] =
    I18n.transliterate(['veteranLastName']).gsub(%r{[^a-zA-Z\-\/\s]}, '').strip.first(50)

  
end

.validate_presence_and_stringiness(value, error_label) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 67

def self.validate_presence_and_stringiness(value, error_label)
  raise ArgumentError, "#{error_label} is missing" unless value
  raise ArgumentError, "#{error_label} is not a string" if value.class != String
end

.validate_source(metadata) ⇒ Object



55
56
57
58
59
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 55

def self.validate_source()
  validate_presence_and_stringiness(['source'], 'source')

  
end

.validate_zip_code(metadata, zip_code_is_us_based) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/simple_forms_api_submission/metadata_validator.rb', line 39

def self.validate_zip_code(, zip_code_is_us_based)
  zip_code = ['zipCode']
  if zip_code_is_us_based
    validate_presence_and_stringiness(zip_code, 'zip code')
    zip_code = zip_code.dup.gsub(/[^0-9]/, '')
    zip_code.insert(5, '-') if zip_code.match?(/\A[0-9]{9}\z/)
    zip_code = '00000' unless zip_code.match?(/\A[0-9]{5}(-[0-9]{4})?\z/)
  else
    zip_code = '00000'
  end

  ['zipCode'] = zip_code

  
end