diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 2e65c89d1..85887c9ee 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -301,8 +301,7 @@ class Procedure < ApplicationRecord validates :lien_site_web, presence: true, if: :publiee? validates :lien_notice, url: { no_local: true, allow_blank: true } - validates :lien_dpo, format: { with: Devise.email_regexp, message: "n'est pas valide" }, if: :lien_dpo_email? - validates :lien_dpo, url: { no_local: true, allow_blank: true }, unless: :lien_dpo_email? + validates :lien_dpo, url: { no_local: true, allow_blank: true, accept_email: true } validates :draft_types_de_champ_public, 'types_de_champ/no_empty_block': true, diff --git a/app/validators/url_validator.rb b/app/validators/url_validator.rb index c24e3590b..fb2f4df02 100644 --- a/app/validators/url_validator.rb +++ b/app/validators/url_validator.rb @@ -14,6 +14,7 @@ class URLValidator < ActiveModel::EachValidator options.reverse_merge!(no_local: false) options.reverse_merge!(public_suffix: false) options.reverse_merge!(accept_array: false) + options.reverse_merge!(accept_email: false) super(options) end @@ -53,15 +54,18 @@ class URLValidator < ActiveModel::EachValidator def validate_url(record, attribute, value, message, schemes) uri = Addressable::URI.parse(value) - host = uri && uri.host - scheme = uri && uri.scheme - valid_scheme = host && scheme && schemes.include?(scheme) - valid_no_local = !options.fetch(:no_local) || (host && host.include?('.')) - valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil)) + unless options.fetch(:accept_email) && uri.path.match?(/^(.+)@(.+)$/) + host = uri && uri.host + scheme = uri && uri.scheme - unless valid_scheme && valid_no_local && valid_suffix - record.errors.add(attribute, message, **filtered_options(value)) + valid_scheme = host && scheme && schemes.include?(scheme) + valid_no_local = !options.fetch(:no_local) || (host && host.include?('.')) + valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil)) + + unless valid_scheme && valid_no_local && valid_suffix + record.errors.add(attribute, message, **filtered_options(value)) + end end rescue Addressable::URI::InvalidURIError record.errors.add(attribute, message, **filtered_options(value)) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 693815b66..9291e15cd 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1598,6 +1598,11 @@ describe Procedure do let(:lien_notice) { 'www.démarches-simplifiées.fr' } it { expect(procedure.valid?).to be_falsey } end + + context 'when an email' do + let(:lien_notice) { 'test@demarches-simplifiees.fr' } + it { expect(procedure.valid?).to be_falsey } + end end describe 'lien_dpo' do @@ -1623,6 +1628,11 @@ describe Procedure do it { expect(procedure.valid?).to be_truthy } end + context 'when valid email with accents' do + let(:lien_dpo) { 'test@démarches-simplifiées.fr' } + it { expect(procedure.valid?).to be_truthy } + end + context 'when not a valid link' do let(:lien_dpo) { 'www.démarches-simplifiées.fr' } it { expect(procedure.valid?).to be_falsey }