discourse/lib/validators/stripped_length_validator.rb
Roman Rizzi e696cba071
FIX: Don't count HTML comments when calculating reply length. (#11658)
We'll remove them when we sanitize the post raw content.
2021-01-07 15:44:17 -03:00

25 lines
975 B
Ruby

# frozen_string_literal: true
class StrippedLengthValidator < ActiveModel::EachValidator
def self.validate(record, attribute, value, range)
if !value.nil?
html_comments_regexp = /<!--(.*?)-->/
stripped_length = value.gsub(html_comments_regexp, '')
stripped_length = stripped_length.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