Class: StrippedLengthValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- StrippedLengthValidator
- Defined in:
- lib/validators/stripped_length_validator.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.get_sanitized_value(value) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/validators/stripped_length_validator.rb', line 29 def self.get_sanitized_value(value) value = value.dup value.gsub!(/<!--(.*?)-->/, "") # strip HTML comments value.gsub!(/:\w+(:\w+)?:/, "X") # replace emojis with a single character value.gsub!(/\.{2,}/, "…") # replace multiple ... with … value.gsub!(/\,{2,}/, ",") # replace multiple ,,, with , value.strip end |
.validate(record, attribute, value, range) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/validators/stripped_length_validator.rb', line 4 def self.validate(record, attribute, value, range) if value.blank? record.errors.add attribute, I18n.t("errors.messages.blank") elsif value.length > range.end record.errors.add attribute, I18n.t( "errors.messages.too_long_validation", count: range.end, length: value.length, ) else value = get_sanitized_value(value) if value.length < range.begin record.errors.add attribute, I18n.t("errors.messages.too_short", count: range.begin) end end end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
23 24 25 26 27 |
# File 'lib/validators/stripped_length_validator.rb', line 23 def validate_each(record, attribute, value) # the `in` parameter might be a lambda when the range is dynamic range = [:in].lambda? ? [:in].call : [:in] self.class.validate(record, attribute, value, range) end |