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.

Move UrlValidator to core.

Sebastian Palmadb9a69b7fb34
diff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rb
index 88f3801002..c9eb975c9d 100644
--- a/api/app/models/spree/webhooks/endpoint.rb
+++ b/api/app/models/spree/webhooks/endpoint.rb
@@ -1,7 +1,7 @@
module Spree
module Webhooks
class Endpoint < Spree::Webhooks::Base
- validates :url, :'spree/webhooks/validators/url' => true
+ validates :url, url: true
end
end
end
diff --git a/api/app/models/spree/webhooks/validators/url_validator.rb b/api/app/models/spree/webhooks/validators/url_validator.rb
deleted file mode 100644
index 61ccc2a694..0000000000
--- a/api/app/models/spree/webhooks/validators/url_validator.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-module Spree
- module Webhooks
- module Validators
- class UrlValidator < ActiveModel::EachValidator
- def validate_each(record, attribute, value)
- unless url_valid?(value)
- record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))
- end
- end
-
- private
-
- ERROR_MESSAGE = 'must be a valid URL'
- private_constant :ERROR_MESSAGE
-
- def url_valid?(url)
- uri = begin
- URI.parse(url)
- rescue URI::InvalidURIError
- return false
- end
- uri.host.present? && uri.kind_of?(URI::HTTP)
- end
- end
- end
- end
-end
diff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rb
index a5254ddca6..b22427db81 100644
--- a/api/spec/models/spree/webhooks/endpoint_spec.rb
+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb
@@ -2,25 +2,13 @@ require 'spec_helper'
describe Spree::Store do
describe 'validations' do
- context 'url presence' do
- it 'is valid with url' do
- endpoint = Spree::Webhooks::Endpoint.new(url: 'https://google.com/')
- expect(endpoint.valid?).to be(true)
- end
-
- it 'is invalid without url' do
- endpoint = Spree::Webhooks::Endpoint.new(url: nil)
- expect(endpoint.valid?).to be(false)
- end
- end
-
context 'url format' do
it 'is invalid with an invalid url' do
endpoint = Spree::Webhooks::Endpoint.new(url: 'google.com')
expect(endpoint.valid?).to be(false)
end
- it 'is valid with an valid url' do
+ it 'is valid with a valid url' do
endpoint = Spree::Webhooks::Endpoint.new(url: 'http://google.com')
expect(endpoint.valid?).to be(true)
end
diff --git a/core/app/validators/url_validator.rb b/core/app/validators/url_validator.rb
new file mode 100644
index 0000000000..6b5a02d60a
--- /dev/null
+++ b/core/app/validators/url_validator.rb
@@ -0,0 +1,21 @@
+class UrlValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ unless url_valid?(value)
+ record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))
+ end
+ end
+
+ private
+
+ ERROR_MESSAGE = 'must be a valid URL'
+ private_constant :ERROR_MESSAGE
+
+ def url_valid?(url)
+ uri = begin
+ URI.parse(url)
+ rescue URI::InvalidURIError
+ return false
+ end
+ uri.host.present? && uri.kind_of?(URI::HTTP)
+ end
+end
diff --git a/api/spec/models/spree/webhooks/validators/url_validator_spec.rb b/core/spec/validators/url_validator_spec.rb
similarity index 87%
rename from api/spec/models/spree/webhooks/validators/url_validator_spec.rb
rename to core/spec/validators/url_validator_spec.rb
index 606b794ba5..f719de7fdb 100644
--- a/api/spec/models/spree/webhooks/validators/url_validator_spec.rb
+++ b/core/spec/validators/url_validator_spec.rb
@@ -4,11 +4,11 @@ module Test
class Product < ActiveRecord::Base
self.table_name = 'test_products'
- validates :url, :'spree/webhooks/validators/url' => true
+ validates :url, url: true
end
end
-describe Spree::Webhooks::Validators::UrlValidator do
+describe UrlValidator do
before(:all) do
ActiveRecord::Base.connection.create_table :test_products, force: true do |t|
t.string :url
@@ -46,14 +46,14 @@ describe Spree::Webhooks::Validators::UrlValidator do
it 'adds the given message to the record url errors array' do
# Remove the validation set at the beginning
Test::Product.clear_validators!
- Test::Product.validates(:url, :'spree/webhooks/validators/url' => { message: message })
+ Test::Product.validates(:url, url: { message: message })
test_product.valid?
expect(test_product.errors.messages).to eq(url: [message])
# Add back the validation removed previously
Test::Product.clear_validators!
- Test::Product.validates(:url, :'spree/webhooks/validators/url' => true)
+ Test::Product.validates(:url, url: true)
end
end