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:
parent
1d66b47300
commit
30bc4aa5d3
7 changed files with 27 additions and 38 deletions
|
@ -540,9 +540,6 @@ module Users
|
||||||
@dossier.check_mandatory_and_visible_champs.map do |error_on_champ|
|
@dossier.check_mandatory_and_visible_champs.map do |error_on_champ|
|
||||||
errors.import(error_on_champ)
|
errors.import(error_on_champ)
|
||||||
end
|
end
|
||||||
@dossier.check_expressions_regulieres_champs.map do |error_on_champ|
|
|
||||||
errors.import(error_on_champ) if error_on_champ.present?
|
|
||||||
end
|
|
||||||
errors
|
errors
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1152,20 +1152,6 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
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)
|
def demander_un_avis!(avis)
|
||||||
log_dossier_operation(avis.claimant, :demander_un_avis, avis)
|
log_dossier_operation(avis.claimant, :demander_un_avis, avis)
|
||||||
end
|
end
|
||||||
|
|
|
@ -618,16 +618,13 @@ 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?
|
||||||
begin
|
return false if expression_reguliere_exemple_text.match?(Regexp.new(expression_reguliere, timeout: 2.0))
|
||||||
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
|
|
||||||
|
|
||||||
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
|
||||||
|
rescue Regexp::TimeoutError
|
||||||
|
self.errors.add(:expression_reguliere, I18n.t('errors.messages.evil_regexp'))
|
||||||
|
true
|
||||||
rescue RegexpError
|
rescue RegexpError
|
||||||
self.errors.add(:expression_reguliere, I18n.t('errors.messages.syntax_error_regexp'))
|
self.errors.add(:expression_reguliere, I18n.t('errors.messages.syntax_error_regexp'))
|
||||||
true
|
true
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
|
|
||||||
class ExpressionReguliereValidator < ActiveModel::Validator
|
class ExpressionReguliereValidator < ActiveModel::Validator
|
||||||
def validate(record)
|
def validate(record)
|
||||||
if record.value.present?
|
if record.value.present?
|
||||||
begin
|
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0))
|
||||||
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))
|
||||||
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'))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
rescue Regexp::TimeoutError
|
||||||
|
record.errors.add(:expression_reguliere, :evil_regexp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
en:
|
||||||
|
activerecord:
|
||||||
|
errors:
|
||||||
|
models:
|
||||||
|
champs/expression_reguliere_champ:
|
||||||
|
attributes:
|
||||||
|
value:
|
||||||
|
invalid_regexp: does not match expected format
|
|
@ -0,0 +1,8 @@
|
||||||
|
fr:
|
||||||
|
activerecord:
|
||||||
|
errors:
|
||||||
|
models:
|
||||||
|
champs/expression_reguliere_champ:
|
||||||
|
attributes:
|
||||||
|
value:
|
||||||
|
invalid_regexp: ne correspond pas au format attendu
|
|
@ -1576,13 +1576,10 @@ describe Dossier, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#check_expressions_regulieres_champs" do
|
describe "#check_expressions_regulieres_champs" do
|
||||||
include Logic
|
|
||||||
|
|
||||||
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
|
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||||
let(:types_de_champ) { [type_de_champ] }
|
let(:types_de_champ) { [type_de_champ] }
|
||||||
let(:type_de_champ) { { type: :expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text: } }
|
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
|
context "with bad example" do
|
||||||
let(:expression_reguliere_exemple_text) { "01234567" }
|
let(:expression_reguliere_exemple_text) { "01234567" }
|
||||||
|
@ -1591,13 +1588,12 @@ describe Dossier, type: :model do
|
||||||
before do
|
before do
|
||||||
champ = dossier.champs_public.first
|
champ = dossier.champs_public.first
|
||||||
champ.value = expression_reguliere_exemple_text
|
champ.value = expression_reguliere_exemple_text
|
||||||
champ.save!
|
dossier.save
|
||||||
dossier.reload
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should have errors' do
|
it 'should have errors' do
|
||||||
expect(errors).not_to be_empty
|
expect(dossier.errors).not_to be_empty
|
||||||
expect(errors.first.full_message).to eq("n'est pas valide")
|
expect(dossier.errors.full_messages.join(',')).to include("ne correspond pas au format attendu")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue