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)
|
|
|
|
if record.value.present?
|
2023-10-24 10:30:12 +02:00
|
|
|
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: TIMEOUT))
|
2023-10-20 01:22:22 +02:00
|
|
|
record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: record.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
|