amelioration(champ.expression_reguliere): pas besoin d'executer la validation du champs expression reguliere dans controller, le validateur le fait sur le champ au moment de sauver le dossier avec le bon context

This commit is contained in:
Martin 2023-10-13 14:27:03 +02:00 committed by Kara Diaby
parent 1d66b47300
commit 30bc4aa5d3
7 changed files with 27 additions and 38 deletions

View file

@ -540,9 +540,6 @@ module Users
@dossier.check_mandatory_and_visible_champs.map do |error_on_champ|
errors.import(error_on_champ)
end
@dossier.check_expressions_regulieres_champs.map do |error_on_champ|
errors.import(error_on_champ) if error_on_champ.present?
end
errors
end

View file

@ -1152,20 +1152,6 @@ class Dossier < ApplicationRecord
end
end
def check_expressions_regulieres_champs
champs_public.filter { _1.expression_reguliere && _1.visible? }.map do |champ|
if champ.value.present?
begin
if !champ.value.match(Regexp.new(champ.expression_reguliere, timeout: 5.0))
champ.errors.add(:value, :invalid)
end
rescue Regexp::TimeoutError
self.errors.add(:value, I18n.t('errors.messages.evil_regexp'))
end
end
end
end
def demander_un_avis!(avis)
log_dossier_operation(avis.claimant, :demander_un_avis, avis)
end

View file

@ -618,16 +618,13 @@ class TypeDeChamp < ApplicationRecord
def invalid_regexp?
return false if expression_reguliere.blank?
return false if expression_reguliere_exemple_text.blank?
begin
if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: 2.0))
return false
end
rescue Regexp::TimeoutError
self.errors.add(:expression_reguliere, I18n.t('errors.messages.evil_regexp'))
end
return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: 2.0))
self.errors.add(:expression_reguliere_exemple_text, I18n.t('errors.messages.mismatch_regexp'))
true
rescue Regexp::TimeoutError
self.errors.add(:expression_reguliere, I18n.t('errors.messages.evil_regexp'))
true
rescue RegexpError
self.errors.add(:expression_reguliere, I18n.t('errors.messages.syntax_error_regexp'))
true

View file

@ -1,14 +1,11 @@
class ExpressionReguliereValidator < ActiveModel::Validator
def validate(record)
if record.value.present?
begin
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0))
record.errors.add(:value, I18n.t('errors.messages.invalid_regexp', expression_reguliere_error_message: record.expression_reguliere_error_message))
end
rescue Regexp::TimeoutError
record.errors.add(:expression_reguliere, I18n.t('errors.messages.evil_regexp'))
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0))
record.errors.add(:value, I18n.t('errors.messages.invalid_regexp', expression_reguliere_error_message: record.expression_reguliere_error_message))
end
end
rescue Regexp::TimeoutError
record.errors.add(:expression_reguliere, :evil_regexp)
end
end

View file

@ -0,0 +1,8 @@
en:
activerecord:
errors:
models:
champs/expression_reguliere_champ:
attributes:
value:
invalid_regexp: does not match expected format

View file

@ -0,0 +1,8 @@
fr:
activerecord:
errors:
models:
champs/expression_reguliere_champ:
attributes:
value:
invalid_regexp: ne correspond pas au format attendu

View file

@ -1576,13 +1576,10 @@ describe Dossier, type: :model do
end
describe "#check_expressions_regulieres_champs" do
include Logic
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:types_de_champ) { [type_de_champ] }
let(:type_de_champ) { { type: :expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text: } }
let(:errors) { dossier.check_expressions_regulieres_champs }
context "with bad example" do
let(:expression_reguliere_exemple_text) { "01234567" }
@ -1591,13 +1588,12 @@ describe Dossier, type: :model do
before do
champ = dossier.champs_public.first
champ.value = expression_reguliere_exemple_text
champ.save!
dossier.reload
dossier.save
end
it 'should have errors' do
expect(errors).not_to be_empty
expect(errors.first.full_message).to eq("n'est pas valide")
expect(dossier.errors).not_to be_empty
expect(dossier.errors.full_messages.join(',')).to include("ne correspond pas au format attendu")
end
end