class Kafo::Validator

Attributes

errors[R]

Public Class Methods

new() click to toggle source
# File lib/kafo/validator.rb, line 5
def initialize
  @errors = []
  @logger = KafoConfigure.logger
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/kafo/validator.rb, line 105
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
validate_absolute_path(args) click to toggle source
# File lib/kafo/validator.rb, line 10
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) click to toggle source
# File lib/kafo/validator.rb, line 20
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) click to toggle source
# File lib/kafo/validator.rb, line 30
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) click to toggle source
# File lib/kafo/validator.rb, line 40
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) click to toggle source
# File lib/kafo/validator.rb, line 50
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) click to toggle source

Non-standard validation is from theforeman/foreman_proxy module

# File lib/kafo/validator.rb, line 70
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) click to toggle source
# File lib/kafo/validator.rb, line 81
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) click to toggle source
# File lib/kafo/validator.rb, line 95
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

Private Instance Methods

error(message) click to toggle source
# File lib/kafo/validator.rb, line 116
def error(message)
  @errors << message
  @logger.error "Validation error: #{message}"
end