Class: DZero::Utils::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/dzero/utils/validator.rb

Constant Summary collapse

CHExtended =

Due to the way classy_hash is written, it cannot be extended conventionally

CH.dup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transmission, validations) ⇒ Validator

Returns a new instance of Validator.



26
27
28
29
# File 'lib/dzero/utils/validator.rb', line 26

def initialize(transmission, validations)
  @transmission = transmission
  @validations  = validations
end

Instance Attribute Details

#transmissionObject (readonly)

Returns the value of attribute transmission.



31
32
33
# File 'lib/dzero/utils/validator.rb', line 31

def transmission
  @transmission
end

#validationsObject (readonly)

Returns the value of attribute validations.



31
32
33
# File 'lib/dzero/utils/validator.rb', line 31

def validations
  @validations
end

Class Method Details

.eq(expected) ⇒ Object



21
22
23
# File 'lib/dzero/utils/validator.rb', line 21

def eq(expected)
  Proc.new { |value| expected == value || "must be #{expected}" }
end

Instance Method Details

#hashObject



57
58
59
# File 'lib/dzero/utils/validator.rb', line 57

def hash
  @hash ||= transmission.to_json(readable: true, key_group_by_segment_sym: true)
end

#readable_errorsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dzero/utils/validator.rb', line 39

def readable_errors
  @errors.map do |error|
    error[:message] = 'must be present' if error[:message] == 'present'

    case error[:full_path][0]
    when :header
      "Transaction Header: #{error[:full_path][1]} #{error[:message]}"
    when :transmission_group
      "#{error[:full_path][1].to_s.titlecase} Segment: #{error[:full_path][2]} #{error[:message]}"
    when :transaction_groups
      next unless error[:full_path][2]
      "Group #{error[:full_path][1]}, #{error[:full_path][2].to_s.titlecase} Segment: #{error[:full_path][3]} #{error[:message]}"
    else
      "#{error[:full_path][-1]} #{error[:message]}"
    end.to_s.strip.squeeze(' ')
  end.compact
end

#validateObject



33
34
35
36
37
# File 'lib/dzero/utils/validator.rb', line 33

def validate
  @errors = []

  CHExtended.validate(hash, validations, errors: @errors, raise_errors: false, full: true)
end