Class: Kafo::Validator

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

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



5
6
7
# File 'lib/kafo/validator.rb', line 5

def initialize
  @logger = KafoConfigure.logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/kafo/validator.rb', line 104

def method_missing(method, *args, &block)
  if method.to_s.start_with?('validate_')
    @logger.debug "Skipping validation with #{method} as it's not implemented in Kafo"
    return true
  else
    super
  end
end

Instance Method Details

#validate_absolute_path(args) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/kafo/validator.rb', line 9

def validate_absolute_path(args)
  args.each do |arg|
    unless arg.to_s.start_with?('/')
      @logger.error "#{arg.inspect} is not an absolute path."
      return false
    end
  end
  return true
end

#validate_array(args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/kafo/validator.rb', line 19

def validate_array(args)
  args.each do |arg|
    unless arg.is_a?(Array)
      @logger.error "#{arg.inspect} is not a valid array."
      return false
    end
  end
  return true
end

#validate_bool(args) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/kafo/validator.rb', line 29

def validate_bool(args)
  args.each do |arg|
    unless arg.is_a?(TrueClass) || arg.is_a?(FalseClass)
      @logger.error "#{arg.inspect} is not a valid boolean."
      return false
    end
  end
  return true
end

#validate_hash(args) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/kafo/validator.rb', line 39

def validate_hash(args)
  args.each do |arg|
    unless arg.is_a?(Hash)
      @logger.error "#{arg.inspect} is not a valid hash."
      return false
    end
  end
  return true
end

#validate_integer(args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kafo/validator.rb', line 49

def validate_integer(args)
  value = args[0]
  max = args[1]
  min = args[2]
  int = Integer(value.to_s)
  if min && int < min.to_i
    @logger.error "#{value} must be at least #{min}."
    return false
  end
  if max && int > max.to_i
    @logger.error "#{value} must be less than #{max}."
    return false
  end
  return true
rescue TypeError, ArgumentError
  @logger.error "#{value.inspect} is not a valid integer."
  return false
end

#validate_listen_on(args) ⇒ Object

Non-standard validation is from theforeman/foreman_proxy module



69
70
71
72
73
74
75
76
77
78
# File 'lib/kafo/validator.rb', line 69

def validate_listen_on(args)
  valid_values = ['http', 'https', 'both']
  args.each do |arg|
    unless valid_values.include?(arg)
      @logger.error "#{arg.inspect} is not a valid value.  Valid values are: #{valid_values.join(", ")}"
      return false
    end
  end
  return true
end

#validate_re(args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kafo/validator.rb', line 80

def validate_re(args)
  value = args[0]
  regexes = args[1]
  regexes = [regexes] unless regexes.is_a?(Array)
  message = args[2] || "#{value.inspect} does not match the accepted inputs: #{regexes.join(", ")}"

  if regexes.any? { |rx| value =~ Regexp.compile(rx) }
    return true
  else
    @logger.error message
    return false
  end
end

#validate_string(args) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/kafo/validator.rb', line 94

def validate_string(args)
  args.each do |arg|
    unless arg.is_a?(String)
      @logger.error "#{arg.inspect} is not a valid string."
      return false
    end
  end
  return true
end