From 5e451b5f8bcd792b942056dbb0ed1a3df78b75f0 Mon Sep 17 00:00:00 2001 From: cesartalves Date: Fri, 9 Apr 2021 16:08:20 -0300 Subject: [PATCH] Add global Spree::Config.default_email_regexp This change adds a `:default_email_regexp` preference to be used by Spree::EmailValidator, allowing the regex to be fully and easily customizable. --- core/lib/spree/app_configuration.rb | 4 ++++ core/lib/spree/core/validators/email.rb | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 13612b1816..79d2d8dce6 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -150,6 +150,10 @@ class AppConfiguration < Preferences::Configuration # @return [String] Two-letter ISO code of a {Spree::Country} to assumed as the country of an unidentified customer (default: "US") preference :default_country_iso, :string, default: 'US' + # @!attribute [rw] default_email_regexp + # @return [Regexp] Regex to be used in email validations, for example in Spree::EmailValidator + preference :default_email_regexp, :regexp, default: URI::MailTo::EMAIL_REGEXP + # @!attribute [rw] generate_api_key_for_all_roles # @return [Boolean] Allow generating api key automatically for user # at role_user creation for all roles. (default: +false+) diff --git a/core/lib/spree/core/validators/email.rb b/core/lib/spree/core/validators/email.rb index 60fe5bcfc8..213a0c4e78 100644 --- a/core/lib/spree/core/validators/email.rb +++ b/core/lib/spree/core/validators/email.rb @@ -13,9 +13,11 @@ module Spree # class EmailValidator < ActiveModel::EachValidator EMAIL_REGEXP = URI::MailTo::EMAIL_REGEXP + # Use Spree::Config.default_email_regexp instead + deprecate_constant :EMAIL_REGEXP def validate_each(record, attribute, value) - unless EMAIL_REGEXP.match? value + unless Spree::Config.default_email_regexp.match? value record.errors.add(attribute, :invalid, **{ value: value }.merge!(options)) end end