Class: Kafo::Validator
- Inherits:
-
Object
show all
- Defined in:
- lib/kafo/validator.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Validator.
6
7
8
9
|
# File 'lib/kafo/validator.rb', line 6
def initialize
@errors = []
@logger = KafoConfigure.logger
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/kafo/validator.rb', line 106
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 Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
4
5
6
|
# File 'lib/kafo/validator.rb', line 4
def errors
@errors
end
|
Instance Method Details
#validate_absolute_path(args) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/kafo/validator.rb', line 11
def validate_absolute_path(args)
args.each do |arg|
unless arg.to_s.start_with?('/')
error "#{arg.inspect} is not an absolute path"
return false
end
end
return true
end
|
#validate_array(args) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/kafo/validator.rb', line 21
def validate_array(args)
args.each do |arg|
unless arg.is_a?(Array)
error "#{arg.inspect} is not a valid array"
return false
end
end
return true
end
|
#validate_bool(args) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/kafo/validator.rb', line 31
def validate_bool(args)
args.each do |arg|
unless arg.is_a?(TrueClass) || arg.is_a?(FalseClass)
error "#{arg.inspect} is not a valid boolean"
return false
end
end
return true
end
|
#validate_hash(args) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/kafo/validator.rb', line 41
def validate_hash(args)
args.each do |arg|
unless arg.is_a?(Hash)
error "#{arg.inspect} is not a valid hash"
return false
end
end
return true
end
|
#validate_integer(args) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/kafo/validator.rb', line 51
def validate_integer(args)
value = args[0]
max = args[1]
min = args[2]
int = Integer(value.to_s)
if min && int < min.to_i
error "#{value} must be at least #{min}"
return false
end
if max && int > max.to_i
error "#{value} must be less than #{max}"
return false
end
return true
rescue TypeError, ArgumentError
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
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/kafo/validator.rb', line 71
def validate_listen_on(args)
valid_values = ['http', 'https', 'both']
args.each do |arg|
unless valid_values.include?(arg)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/kafo/validator.rb', line 82
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
error message
return false
end
end
|
#validate_string(args) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/kafo/validator.rb', line 96
def validate_string(args)
args.each do |arg|
unless arg.is_a?(String)
error "#{arg.inspect} is not a valid string"
return false
end
end
return true
end
|