From 29d36973baa5a5dc8a46855062e3c302f43385e2 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 15 Jul 2024 09:54:44 +0200 Subject: [PATCH 1/2] bug(tdc.expression_reguliere): invalid regexp should not raise error --- spec/components/procedures/errors_summary_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/components/procedures/errors_summary_spec.rb b/spec/components/procedures/errors_summary_spec.rb index ebeb1096d..a0a646167 100644 --- a/spec/components/procedures/errors_summary_spec.rb +++ b/spec/components/procedures/errors_summary_spec.rb @@ -50,7 +50,8 @@ describe Procedure::ErrorsSummary, type: :component do { libelle: 'repetition requires children', type: :repetition, children: [] }, { libelle: 'drop down list requires options', type: :drop_down_list, options: [] }, { libelle: 'invalid condition', type: :text, condition: ds_eq(constant(true), constant(1)) }, - { libelle: 'header sections must have consistent order', type: :header_section, level: 2 } + { libelle: 'header sections must have consistent order', type: :header_section, level: 2 }, + { libelle: 'regexp invalid', type: :expression_reguliere, expression_reguliere_exemple_text: 'kthxbye', expression_reguliere: /{/ } ]) end From 19268345db89553252f477c3f0fd90a7b12bd54f Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 15 Jul 2024 09:59:50 +0200 Subject: [PATCH 2/2] fix(tdc.expression_reguliere): invalid regexp shows nice error message --- .../types_de_champ/expression_reguliere_validator.rb | 6 +++++- config/locales/models/procedure/en.yml | 2 ++ config/locales/models/procedure/fr.yml | 2 ++ spec/components/procedures/errors_summary_spec.rb | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/validators/types_de_champ/expression_reguliere_validator.rb b/app/validators/types_de_champ/expression_reguliere_validator.rb index 39262adeb..3aaef80e7 100644 --- a/app/validators/types_de_champ/expression_reguliere_validator.rb +++ b/app/validators/types_de_champ/expression_reguliere_validator.rb @@ -4,7 +4,11 @@ class TypesDeChamp::ExpressionReguliereValidator < ActiveModel::EachValidator .flat_map { _1.repetition? ? procedure.draft_revision.children_of(_1) : _1 } .each do |tdc| if tdc.expression_reguliere? && tdc.invalid_regexp? - procedure.errors.add(:expression_reguliere, type_de_champ: tdc) + procedure.errors.add( + attribute, + procedure.errors.generate_message(attribute, :expression_reguliere_invalid, { value: tdc.libelle }), + type_de_champ: tdc + ) end end end diff --git a/config/locales/models/procedure/en.yml b/config/locales/models/procedure/en.yml index 34fc89e35..1328196b5 100644 --- a/config/locales/models/procedure/en.yml +++ b/config/locales/models/procedure/en.yml @@ -76,12 +76,14 @@ en: empty_repetition: 'requires at least one field' empty_drop_down: 'requires at least one option' inconsistent_header_section: "%{custom_message}" + expression_reguliere_invalid: "is invalid" draft_types_de_champ_private: format: 'Private field %{message}' invalid_condition: "have an invalid logic" empty_repetition: 'requires at least one field' empty_drop_down: 'requires at least one option' inconsistent_header_section: "%{custom_message}" + expression_reguliere_invalid: "is invalid" attestation_template: format: "%{attribute} %{message}" initiated_mail: diff --git a/config/locales/models/procedure/fr.yml b/config/locales/models/procedure/fr.yml index 5a9dfd9ab..35713d133 100644 --- a/config/locales/models/procedure/fr.yml +++ b/config/locales/models/procedure/fr.yml @@ -82,12 +82,14 @@ fr: empty_repetition: 'doit comporter au moins un champ répétable' empty_drop_down: 'doit comporter au moins un choix sélectionnable' inconsistent_header_section: "%{custom_message}" + expression_reguliere_invalid: "est invalide, veuillez la corriger" draft_types_de_champ_private: format: 'L’annotation privée %{message}' invalid_condition: "a une logique conditionnelle invalide" empty_repetition: 'doit comporter au moins un champ répétable' empty_drop_down: 'doit comporter au moins un choix sélectionnable' inconsistent_header_section: "%{custom_message}" + expression_reguliere_invalid: "est invalide, veuillez la corriger" attestation_template: format: "%{attribute} %{message}" initiated_mail: diff --git a/spec/components/procedures/errors_summary_spec.rb b/spec/components/procedures/errors_summary_spec.rb index a0a646167..06bf2c145 100644 --- a/spec/components/procedures/errors_summary_spec.rb +++ b/spec/components/procedures/errors_summary_spec.rb @@ -71,6 +71,9 @@ describe Procedure::ErrorsSummary, type: :component do expect(page).to have_selector("a", text: "header sections must have consistent order") expect(page).to have_content("devrait être précédé d'un titre de niveau 1") + + expect(page).to have_selector("a", text: "regexp invalid") + expect(page).to have_content("est invalide, veuillez la corriger") end end