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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Move UrlValidator to core.
Sebastian Palmadb9a69b7fb34
diff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbindex 88f3801002..c9eb975c9d 100644--- a/api/app/models/spree/webhooks/endpoint.rb+++ b/api/app/models/spree/webhooks/endpoint.rb@@ -1,7 +1,7 @@module Spreemodule Webhooksclass Endpoint < Spree::Webhooks::Base- validates :url, :'spree/webhooks/validators/url' => true+ validates :url, url: trueendendenddiff --git a/api/app/models/spree/webhooks/validators/url_validator.rb b/api/app/models/spree/webhooks/validators/url_validator.rbdeleted file mode 100644index 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-enddiff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rbindex 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 dodescribe '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' doit 'is invalid with an invalid url' doendpoint = 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' doendpoint = Spree::Webhooks::Endpoint.new(url: 'http://google.com')expect(endpoint.valid?).to be(true)enddiff --git a/core/app/validators/url_validator.rb b/core/app/validators/url_validator.rbnew file mode 100644index 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+enddiff --git a/api/spec/models/spree/webhooks/validators/url_validator_spec.rb b/core/spec/validators/url_validator_spec.rbsimilarity index 87%rename from api/spec/models/spree/webhooks/validators/url_validator_spec.rbrename to core/spec/validators/url_validator_spec.rbindex 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 Testclass Product < ActiveRecord::Baseself.table_name = 'test_products'- validates :url, :'spree/webhooks/validators/url' => true+ validates :url, url: trueendend-describe Spree::Webhooks::Validators::UrlValidator do+describe UrlValidator dobefore(:all) doActiveRecord::Base.connection.create_table :test_products, force: true do |t|t.string :url@@ -46,14 +46,14 @@ describe Spree::Webhooks::Validators::UrlValidator doit 'adds the given message to the record url errors array' do# Remove the validation set at the beginningTest::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 previouslyTest::Product.clear_validators!- Test::Product.validates(:url, :'spree/webhooks/validators/url' => true)+ Test::Product.validates(:url, url: true)endend