fix(procedure): accept dpo emails with accents
This commit is contained in:
parent
361f6e67c5
commit
08f0184700
3 changed files with 22 additions and 9 deletions
|
@ -301,8 +301,7 @@ class Procedure < ApplicationRecord
|
||||||
|
|
||||||
validates :lien_site_web, presence: true, if: :publiee?
|
validates :lien_site_web, presence: true, if: :publiee?
|
||||||
validates :lien_notice, url: { no_local: true, allow_blank: true }
|
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, accept_email: true }
|
||||||
validates :lien_dpo, url: { no_local: true, allow_blank: true }, unless: :lien_dpo_email?
|
|
||||||
|
|
||||||
validates :draft_types_de_champ_public,
|
validates :draft_types_de_champ_public,
|
||||||
'types_de_champ/no_empty_block': true,
|
'types_de_champ/no_empty_block': true,
|
||||||
|
|
|
@ -14,6 +14,7 @@ class URLValidator < ActiveModel::EachValidator
|
||||||
options.reverse_merge!(no_local: false)
|
options.reverse_merge!(no_local: false)
|
||||||
options.reverse_merge!(public_suffix: false)
|
options.reverse_merge!(public_suffix: false)
|
||||||
options.reverse_merge!(accept_array: false)
|
options.reverse_merge!(accept_array: false)
|
||||||
|
options.reverse_merge!(accept_email: false)
|
||||||
|
|
||||||
super(options)
|
super(options)
|
||||||
end
|
end
|
||||||
|
@ -53,15 +54,18 @@ class URLValidator < ActiveModel::EachValidator
|
||||||
|
|
||||||
def validate_url(record, attribute, value, message, schemes)
|
def validate_url(record, attribute, value, message, schemes)
|
||||||
uri = Addressable::URI.parse(value)
|
uri = Addressable::URI.parse(value)
|
||||||
host = uri && uri.host
|
|
||||||
scheme = uri && uri.scheme
|
|
||||||
|
|
||||||
valid_scheme = host && scheme && schemes.include?(scheme)
|
unless options.fetch(:accept_email) && uri.path.match?(/^(.+)@(.+)$/)
|
||||||
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
|
host = uri && uri.host
|
||||||
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil))
|
scheme = uri && uri.scheme
|
||||||
|
|
||||||
unless valid_scheme && valid_no_local && valid_suffix
|
valid_scheme = host && scheme && schemes.include?(scheme)
|
||||||
record.errors.add(attribute, message, **filtered_options(value))
|
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
|
end
|
||||||
rescue Addressable::URI::InvalidURIError
|
rescue Addressable::URI::InvalidURIError
|
||||||
record.errors.add(attribute, message, **filtered_options(value))
|
record.errors.add(attribute, message, **filtered_options(value))
|
||||||
|
|
|
@ -1598,6 +1598,11 @@ describe Procedure do
|
||||||
let(:lien_notice) { 'www.démarches-simplifiées.fr' }
|
let(:lien_notice) { 'www.démarches-simplifiées.fr' }
|
||||||
it { expect(procedure.valid?).to be_falsey }
|
it { expect(procedure.valid?).to be_falsey }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when an email' do
|
||||||
|
let(:lien_notice) { 'test@demarches-simplifiees.fr' }
|
||||||
|
it { expect(procedure.valid?).to be_falsey }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'lien_dpo' do
|
describe 'lien_dpo' do
|
||||||
|
@ -1623,6 +1628,11 @@ describe Procedure do
|
||||||
it { expect(procedure.valid?).to be_truthy }
|
it { expect(procedure.valid?).to be_truthy }
|
||||||
end
|
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
|
context 'when not a valid link' do
|
||||||
let(:lien_dpo) { 'www.démarches-simplifiées.fr' }
|
let(:lien_dpo) { 'www.démarches-simplifiées.fr' }
|
||||||
it { expect(procedure.valid?).to be_falsey }
|
it { expect(procedure.valid?).to be_falsey }
|
||||||
|
|
Loading…
Reference in a new issue