2023-09-29 23:09:41 +02:00
|
|
|
class ExpressionReguliereValidator < ActiveModel::Validator
|
2023-10-24 10:30:12 +02:00
|
|
|
TIMEOUT = 1.second.freeze
|
|
|
|
|
2023-09-29 23:09:41 +02:00
|
|
|
def validate(record)
|
2023-10-20 17:34:59 +02:00
|
|
|
expression_reguliere = options[:expression_reguliere] || record.expression_reguliere
|
|
|
|
expression_reguliere_error_message = options[:expression_reguliere_error_message] || record.expression_reguliere_error_message
|
|
|
|
|
2023-09-29 23:09:41 +02:00
|
|
|
if record.value.present?
|
2023-10-20 17:34:59 +02:00
|
|
|
if !record.value.match?(Regexp.new(expression_reguliere, timeout: TIMEOUT))
|
|
|
|
record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: expression_reguliere_error_message)
|
2023-09-29 23:09:41 +02:00
|
|
|
end
|
|
|
|
end
|
2023-10-13 14:27:03 +02:00
|
|
|
rescue Regexp::TimeoutError
|
|
|
|
record.errors.add(:expression_reguliere, :evil_regexp)
|
2023-09-29 23:09:41 +02:00
|
|
|
end
|
|
|
|
end
|