discourse/lib/validators/integer_setting_validator.rb

28 lines
902 B
Ruby
Raw Normal View History

class IntegerSettingValidator
2017-07-27 21:20:09 -04:00
def initialize(opts = {})
@opts = opts
@opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden]
# set max closer to a long int
@opts[:max] = 2_000_000_000 unless @opts[:max].present? || @opts[:hidden]
end
def valid_value?(val)
return false if val.to_i.to_s != val.to_s
2017-07-27 21:20:09 -04:00
return false if @opts[:min] && @opts[:min].to_i > (val.to_i)
return false if @opts[:max] && @opts[:max].to_i < (val.to_i)
true
end
def error_message
if @opts[:min] && @opts[:max]
2017-07-27 21:20:09 -04:00
I18n.t('site_settings.errors.invalid_integer_min_max', min: @opts[:min], max: @opts[:max])
elsif @opts[:min]
2017-07-27 21:20:09 -04:00
I18n.t('site_settings.errors.invalid_integer_min', min: @opts[:min])
elsif @opts[:max]
2017-07-27 21:20:09 -04:00
I18n.t('site_settings.errors.invalid_integer_max', max: @opts[:max])
else
I18n.t('site_settings.errors.invalid_integer')
end
end
end