mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 11:44:44 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
23 lines
897 B
Ruby
23 lines
897 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Validators; end
|
|
class Validators::StrippedLengthValidator < ActiveModel::EachValidator
|
|
def self.validate(record, attribute, value, range)
|
|
unless value.nil?
|
|
stripped_length = value.strip.length
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) unless
|
|
stripped_length >= range.begin
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_long_validation', max: range.end, length: stripped_length)) unless
|
|
stripped_length <= range.end
|
|
else
|
|
record.errors.add attribute, (I18n.t('errors.messages.blank'))
|
|
end
|
|
end
|
|
|
|
def validate_each(record, attribute, value)
|
|
# the `in` parameter might be a lambda when the range is dynamic
|
|
range = options[:in].lambda? ? options[:in].call : options[:in]
|
|
self.class.validate(record, attribute, value, range)
|
|
end
|
|
end
|