2018-09-11 05:05:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-30 08:53:06 -04:00
|
|
|
class JsRegexValidator < ActiveModel::EachValidator
|
|
|
|
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
|
|
|
|
end
|