Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

ADD phone validator dependency (#12872)

brozek157b421ec51e
diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb
index 0bf3f304d2..22045673a3 100644
--- a/core/app/models/spree/address.rb
+++ b/core/app/models/spree/address.rb
@@ -65,6 +65,7 @@ module Spree
end
validate :state_validate, :postal_code_validate
+ validate :address_validators, on: [:create, :update]
validates :label, uniqueness: { conditions: -> { where(deleted_at: nil) },
scope: :user_id,
@@ -72,6 +73,12 @@ module Spree
allow_blank: true,
allow_nil: true }
+ def address_validators
+ Rails.application.config.spree.validators.addresses.each do |validator|
+ validates_with validator
+ end
+ end
+
delegate :name, :iso3, :iso, :iso_name, to: :country, prefix: true
delegate :abbr, to: :state, prefix: true, allow_nil: true
diff --git a/core/app/services/spree/addresses/phone_validator.rb b/core/app/services/spree/addresses/phone_validator.rb
new file mode 100644
index 0000000000..ab8a1a7103
--- /dev/null
+++ b/core/app/services/spree/addresses/phone_validator.rb
@@ -0,0 +1,20 @@
+# This validator can be overridden to implement custom phone validation logic.
+# Currently uses the phonelib gem to validate phone numbers and ensure they
+# belong to the address country.
+
+require 'phonelib'
+
+module Spree
+ module Addresses
+ class PhoneValidator < ActiveModel::Validator
+ def validate(address)
+ return if !Spree::Config[:address_requires_phone] || address.phone.blank? || address.country.blank? || address.country_iso.blank?
+
+ phone = Phonelib.parse(address.phone)
+ unless phone.valid_for_country?(address.country_iso)
+ address.errors.add(:phone, :invalid)
+ end
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index c288fdf1e2..3ef5a2e4e5 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -5,6 +5,7 @@ module Spree
module Core
class Engine < ::Rails::Engine
Environment = Struct.new(:calculators,
+ :validators,
:preferences,
:dependencies,
:payment_methods,
@@ -27,6 +28,7 @@ module Spree
:integrations)
SpreeCalculators = Struct.new(:shipping_methods, :tax_rates, :promotion_actions_create_adjustments, :promotion_actions_create_item_adjustments)
PromoEnvironment = Struct.new(:rules, :actions)
+ SpreeValidators = Struct.new(:addresses)
isolate_namespace Spree
engine_name 'spree'
@@ -35,7 +37,7 @@ module Spree
end
initializer 'spree.environment', before: :load_config_initializers do |app|
- app.config.spree = Environment.new(SpreeCalculators.new, Spree::Core::Configuration.new, Spree::Core::Dependencies.new)
+ app.config.spree = Environment.new(SpreeCalculators.new, SpreeValidators.new, Spree::Core::Configuration.new, Spree::Core::Dependencies.new)
app.config.active_record.yaml_column_permitted_classes ||= []
app.config.active_record.yaml_column_permitted_classes.concat([Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess])
@@ -272,6 +274,10 @@ module Spree
Rails.application.config.spree.analytics_event_handlers = []
Rails.application.config.spree.integrations = []
+
+ Rails.application.config.spree.validators.addresses = [
+ Spree::Addresses::PhoneValidator
+ ]
end
initializer 'spree.promo.register.promotions.actions' do |app|
diff --git a/core/spec/models/spree/address_spec.rb b/core/spec/models/spree/address_spec.rb
index 0ce1a63518..4a0f348044 100644
--- a/core/spec/models/spree/address_spec.rb
+++ b/core/spec/models/spree/address_spec.rb
@@ -699,4 +699,12 @@ describe Spree::Address, type: :model do
end
end
end
+
+ context 'address validators' do
+ it 'runs through all configured validators during validation' do
+ address = create(:address)
+ expect_any_instance_of(Spree::Addresses::PhoneValidator).to receive(:validate).with(address)
+ address.valid?
+ end
+ end
end
diff --git a/core/spec/services/spree/addresses/phone_validator_spec.rb b/core/spec/services/spree/addresses/phone_validator_spec.rb
new file mode 100644
index 0000000000..06ffdcd0aa
--- /dev/null
+++ b/core/spec/services/spree/addresses/phone_validator_spec.rb
@@ -0,0 +1,150 @@
+require 'spec_helper'
+
+describe Spree::Addresses::PhoneValidator do
+ subject { described_class.new }
+
+ shared_examples 'does not add a phone error' do
+ it 'does not add an error' do
+ subject.validate(address)
+ expect(address.errors[:phone]).to be_empty
+ end
+ end
+
+ before do
+ Spree::Config[:address_requires_phone] = true
+ end
+
+ describe '#validate' do
+ context 'when no phone is provided' do
+ let(:address) { create(:address, phone: nil) }
+
+ before do
+ Spree::Config[:address_requires_phone] = false
+ end
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'when no country is provided' do
+ let(:address) { build_stubbed(:address, phone: '2025550123', country: nil) }
+
+ before do
+ Spree::Config[:address_requires_phone] = false
+ end
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'when no country iso is provided' do
+ let(:address) { build_stubbed(:address, phone: '2025550123') }
+
+ before do
+ Spree::Config[:address_requires_phone] = false
+ address.country.iso = nil
+ end
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'when phone is provided' do
+ context 'and country is US' do
+ context 'and phone is valid' do
+ let(:address) { create(:address, phone: phone) }
+
+ context 'without prefix' do
+ let(:phone) { '2025550123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has prefix with plus sign' do
+ let(:phone) { '+12025550123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has prefix without plus sign' do
+ let(:phone) { '12025550123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashes' do
+ let(:phone) { '202-555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashes and prefix' do
+ let(:phone) { '+1202-555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashes and prefix without plus sign' do
+ let(:phone) { '1202-555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashes and prefix after dash' do
+ let(:phone) { '1-202-555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashed and parenthesized' do
+ let(:phone) { '(202) 555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashed and parenthesized and prefix' do
+ let(:phone) { '+1(202) 555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+
+ context 'and has dashed and parenthesized and prefix without plus sign' do
+ let(:phone) { '1(202) 555-0123' }
+
+ it_behaves_like 'does not add a phone error'
+ end
+ end
+
+ context 'and phone is invalid' do
+ let(:address) { build_stubbed(:address, phone: phone) }
+
+ context 'and phone is from another country' do
+ let(:phone) { '+48587530227' } # Poland
+
+ it 'adds an error' do
+ subject.validate(address)
+ expect(address.errors[:phone]).to include('is invalid')
+ end
+ end
+
+ context 'because not matching US area code' do
+ let(:phone) { '1234567890' }
+
+ it 'adds an error' do
+ subject.validate(address)
+ expect(address.errors[:phone]).to include('is invalid')
+ end
+ end
+ end
+ end
+
+ context 'because country is not matching phone' do
+ let(:phone) { '+1(202) 555-0123' } # US
+ let(:country) { create(:country, iso: 'PL') }
+ let(:address) { build_stubbed(:address, phone: phone, country: country) }
+
+ it 'adds an error' do
+ subject.validate(address)
+ expect(address.errors[:phone]).to include('is invalid')
+ end
+ end
+ end
+ end
+end
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index f89dafb35e..dbe566cb25 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -64,4 +64,5 @@ Gem::Specification.new do |s|
s.add_dependency 'wannabe_bool'
s.add_dependency 'any_ascii', '~> 0.3.2'
s.add_dependency 'safely_block', '~> 0.4'
+ s.add_dependency 'phonelib', '~> 0.10'
end