fix(champ regex): same timeout across validations, and more agressive value

This commit is contained in:
Colin Darie 2023-10-24 10:30:12 +02:00
parent dbd0e82b09
commit f4e3d2137b
2 changed files with 4 additions and 2 deletions

View file

@ -618,7 +618,7 @@ class TypeDeChamp < ApplicationRecord
def invalid_regexp? def invalid_regexp?
return false if expression_reguliere.blank? return false if expression_reguliere.blank?
return false if expression_reguliere_exemple_text.blank? return false if expression_reguliere_exemple_text.blank?
return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: 2.0)) return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: ExpressionReguliereValidator::TIMEOUT))
self.errors.add(:expression_reguliere_exemple_text, I18n.t('errors.messages.mismatch_regexp')) self.errors.add(:expression_reguliere_exemple_text, I18n.t('errors.messages.mismatch_regexp'))
true true

View file

@ -1,7 +1,9 @@
class ExpressionReguliereValidator < ActiveModel::Validator class ExpressionReguliereValidator < ActiveModel::Validator
TIMEOUT = 1.second.freeze
def validate(record) def validate(record)
if record.value.present? if record.value.present?
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0)) if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: TIMEOUT))
record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: record.expression_reguliere_error_message) record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: record.expression_reguliere_error_message)
end end
end end