Class: JsRegexValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/js_regex_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/validators/js_regex_validator.rb', line 4

def validate_each(record, attribute, value)
  return true if value.blank?

  parsed_regex = JsRegex.new(Regexp.new(value, Regexp::IGNORECASE))

  if parsed_regex.source.empty?
    record.errors.add(attribute, "Regex Pattern #{value} can not be expressed in Javascript")
  else
    parsed_regex.warnings.each { |warning| record.errors.add(attribute, warning) }
  end
rescue RegexpError => regex_error
  record.errors.add(attribute, regex_error.to_s)
end