feat(email.validation): expand email validation to Avis, ContactInformation, Invite, DossierTransfert

This commit is contained in:
Martin 2024-02-13 10:37:03 +01:00 committed by mfo
parent 05193e1d1e
commit 5f77c0cd06
18 changed files with 45 additions and 31 deletions

View file

@ -22,7 +22,7 @@ class SimpleFormatComponent < ApplicationComponent
}
SIMPLE_URL_REGEX = %r{https?://[^\s<>]+}
EMAIL_IN_TEXT_REGEX = Regexp.new(Devise.email_regexp.source.gsub(/\\A|\\z/, '\b'))
EMAIL_IN_TEXT_REGEX = Regexp.new(StrictEmailValidator::REGEXP.source.gsub(/\\A|\\z/, '\b'))
def initialize(text, allow_a: true, allow_autolink: true, class_names_map: {})
@allow_a = allow_a

View file

@ -29,7 +29,7 @@ class Users::SessionsController < Devise::SessionsController
end
def link_sent
if Devise.email_regexp.match?(params[:email])
if StrictEmailValidator::REGEXP.match?(params[:email])
@email = params[:email]
else
redirect_to root_path

View file

@ -21,7 +21,7 @@ class Avis < ApplicationRecord
content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: FILE_MAX_SIZE }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :email, strict_email: true, allow_nil: true
validates :question_answer, inclusion: { in: [true, false] }, on: :update, if: -> { question_label.present? }
validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, size: { less_than: FILE_MAX_SIZE }

View file

@ -11,7 +11,7 @@ module UserFindByConcern
end
def self.find_all_by_identifier_with_emails(ids: [], emails: [])
valid_emails, invalid_emails = emails.partition { Devise.email_regexp.match?(_1) }
valid_emails, invalid_emails = emails.partition { StrictEmailValidator::REGEXP.match?(_1) }
[
where(id: ids).or(where(users: { email: valid_emails })).distinct(:id),

View file

@ -3,7 +3,7 @@ class ContactInformation < ApplicationRecord
validates :nom, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :nom, uniqueness: { scope: :groupe_instructeur, message: 'existe déjà' }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :email, strict_email: true, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :telephone, phone: { possible: true, allow_blank: false }
validates :horaires, presence: { message: 'doivent être renseignés' }, allow_nil: false
validates :adresse, presence: { message: 'doit être renseignée' }, allow_nil: false

View file

@ -4,7 +4,7 @@ class DossierTransfer < ApplicationRecord
EXPIRATION_LIMIT = 2.weeks
validates :email, format: { with: Devise.email_regexp }
validates :email, strict_email: true, presence: true
before_validation -> { sanitize_email(:email) }
scope :pending, -> { where('created_at > ?', (Time.zone.now - EXPIRATION_LIMIT)) }

View file

@ -11,8 +11,7 @@ class Invite < ApplicationRecord
validates :email, presence: true
validates :email, uniqueness: { scope: :dossier_id }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :email, strict_email: true, allow_nil: true
scope :with_dossiers, -> { joins(:dossier).merge(Dossier.visible_by_user) }

View file

@ -37,10 +37,8 @@ class User < ApplicationRecord
before_validation -> { sanitize_email(:email) }
validate :does_not_merge_on_self, if: :requested_merge_into_id_changed?
with_options if: :elligible_to_new_validation? do
before_validation :remove_devise_email_validator
validates :email, strict_email: true
end
before_validation :remove_devise_email_validator
validates :email, strict_email: true
def validate_password_complexity?
administrateur?
@ -274,10 +272,6 @@ class User < ApplicationRecord
Invite.where(email: email).update_all(user_id: id)
end
def elligible_to_new_validation?
StrictEmailValidator.elligible_to_new_validation?(self)
end
def remove_devise_email_validator
_validators[:email]&.reject! { _1.is_a?(ActiveModel::Validations::FormatValidator) }
_validate_callbacks.each do |callback|