Project

General

Profile

Download (1.25 KB) Statistics
| Branch: | Tag: | Revision:
module Foreman
module Parameters
class Validator
KEY_DELM = ","

def initialize(item, options = {})
@item, @options = item, options
end

def validate!
case @options[:type].to_s
when "regexp"
validate_regexp
when "list", "array"
validate_list
else
return true
end
end

private

def value
@item.send(@options[:getter])
end

def validate_regexp
return true if contains_erb?(value) && Setting[:interpolate_erb_in_parameters]

unless value =~ /#{@options[:validate_with]}/
add_error(_("is invalid"))
return false
end
true
end

def validate_list
return true if contains_erb?(value) && Setting[:interpolate_erb_in_parameters]

unless @options[:validate_with].split(KEY_DELM).map(&:strip).include?(value)
add_error(_("%{value} is not one of %{rules}") % { :value => value, :rules => @options[:validate_with] })
return false
end
true
end

def add_error(message)
@item.errors.add(@options[:getter], message)
end

def contains_erb?(value)
value =~ /<%.*%>/
end
end
end
end
(2-2/2)