Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6.10']
ruby-version: ['2.7.8', '3.0.7']
steps:
- uses: actions/checkout@v3
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1.133.0
uses: ruby/setup-ruby@v1.218.0
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
Expand All @@ -24,14 +24,14 @@ jobs:
name: linters / rubocop
runs-on: ubuntu-latest
env:
RUBY_VERSION: 2.6.10
RUBY_VERSION: 2.7.8
RUBOCOP_VERSION: 1.41.1
steps:
- name: Codebase Checkout
uses: actions/checkout@v3

- name: Setup Ruby
uses: ruby/setup-ruby@v1.133.0
uses: ruby/setup-ruby@v1.218.0
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7
103 changes: 52 additions & 51 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
PATH
remote: .
specs:
validate_url (1.0.3)
activemodel (>= 3.0.0)
validate_url (1.0.4)
activemodel (>= 3.0.0, < 7.0)
addressable
public_suffix (~> 2.x.x)
public_suffix (~> 4.0.0)

GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.2)
activesupport (= 5.2.2)
activerecord (5.2.2)
activemodel (= 5.2.2)
activesupport (= 5.2.2)
arel (>= 9.0)
activesupport (5.2.2)
activemodel (6.1.7.10)
activesupport (= 6.1.7.10)
activerecord (6.1.7.10)
activemodel (= 6.1.7.10)
activesupport (= 6.1.7.10)
activesupport (6.1.7.10)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (9.0.0)
builder (3.2.3)
concurrent-ruby (1.1.3)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.2.0)
builder (3.3.0)
concurrent-ruby (1.3.4)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
git (1.5.0)
github_api (0.11.3)
addressable (~> 2.3)
descendants_tracker (~> 0.0.1)
faraday (~> 0.8, < 0.10)
hashie (>= 1.2)
multi_json (>= 1.7.5, < 2.0)
nokogiri (~> 1.6.0)
oauth2
hashie (3.6.0)
github_api (0.18.1)
addressable (~> 2.4)
descendants_tracker (~> 0.0.4)
faraday (~> 0.8)
hashie (>= 3.4)
oauth2 (~> 1.0)
hashie (5.0.0)
highline (2.0.0)
i18n (1.2.0)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
jeweler (2.3.5)
jeweler (2.1.1)
builder
bundler (>= 1.0)
git (>= 1.2.5)
github_api (~> 0.11.0)
github_api
highline (>= 1.6.15)
nokogiri (>= 1.5.10)
psych (~> 2.2)
rake
rdoc
semver2
jwt (2.1.0)
mini_portile2 (2.1.0)
minitest (5.11.3)
multi_json (1.13.1)
semver
jwt (2.10.1)
base64
mini_portile2 (2.8.8)
minitest (5.25.4)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.0.0)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
multipart-post (2.4.1)
nokogiri (1.15.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
oauth2 (1.4.8)
faraday (>= 0.8, < 3.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
psych (2.2.4)
public_suffix (2.0.5)
rack (2.2.8)
public_suffix (4.0.7)
racc (1.8.1)
rack (2.2.10)
rake (12.3.0)
rdoc (6.0.4)
rspec (3.8.0)
Expand All @@ -86,23 +85,25 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
semver2 (3.4.2)
sqlite3 (1.3.13)
semver (1.0.1)
sqlite3 (1.6.9)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
zeitwerk (2.6.18)

PLATFORMS
ruby

DEPENDENCIES
activerecord
activerecord (< 7.0)
concurrent-ruby (= 1.3.4)
diff-lcs (>= 1.1.2)
jeweler
rake
rspec (>= 3.0.0)
sqlite3
sqlite3 (= 1.6.9)
validate_url!

BUNDLED WITH
1.17.1
2.4.15
2 changes: 1 addition & 1 deletion VERSION.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
:major: 1
:minor: 0
:patch: 2
:patch: 4
:build:
25 changes: 14 additions & 11 deletions lib/validate_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
require 'active_model'
require 'active_support/i18n'
require 'public_suffix'
I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
I18n.load_path += Dir[File.dirname(__FILE__) + '/locale/*.yml']

module ActiveModel
module Validations
class UrlValidator < ActiveModel::EachValidator
def initialize(options)
options.reverse_merge!(:schemes => %w(http https))
options.reverse_merge!(:no_local => false)
options.reverse_merge!(:public_suffix => false)
options.reverse_merge!(:no_pre_query => false)
options.reverse_merge!(schemes: %w[http https])
options.reverse_merge!(no_local: false)
options.reverse_merge!(public_suffix: false)
options.reverse_merge!(no_pre_query: false)

super(options)
end
Expand All @@ -33,17 +33,21 @@ def validate_each(record, attribute, value)
add_error(options.fetch(:message, :url_host)) unless validate_host_presence(uri)
add_error(options.fetch(:message, :url_suffix)) unless validate_suffix(uri, public_suffix)
add_error(options.fetch(:message, :url_suffix)) unless validate_no_local(uri, no_local)
add_error(*options.fetch(:message, [:url_scheme, :schemes => schemes.join(', ')])) unless validate_scheme_presence(uri, schemes)
unless validate_scheme_presence(uri, schemes)
add_error(*options.fetch(:message, [:url_scheme, schemes: schemes.join(', ')]))
end
add_error(options.fetch(:message, :url_path)) unless validate_pre_query(uri, no_pre_query)

rescue Addressable::URI::InvalidURIError
add_error(options.fetch(:message, :url))
end
end

def add_error(*message)
return if @errors.include?(message)
@record.errors.add(@attribute, *message)

arg = message.slice(0)
kwargs = message.slice(1) || {}
@record.errors.add(@attribute, arg, **kwargs)
@errors << message
end

Expand All @@ -52,7 +56,7 @@ def validate_host_presence(uri)
end

def validate_suffix(uri, public_suffix)
!public_suffix || (PublicSuffix.valid?(uri.host, :default_rule => nil))
!public_suffix || PublicSuffix.valid?(uri.host, default_rule: nil)
end

def validate_no_local(uri, no_local)
Expand All @@ -65,9 +69,8 @@ def validate_scheme_presence(uri, schemes)

def validate_pre_query(uri, no_pre_query)
# URLs with queries should have a '/' before the '?'.
no_pre_query || uri.query.nil? || uri.path&.starts_with?('/')
no_pre_query || uri.query.nil? || uri.path&.start_with?('/')
end

end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion spec/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class User

attr_accessor :homepage

validates :homepage, :url => true
validates :homepage, url: true
end
4 changes: 2 additions & 2 deletions spec/resources/user_with_ar.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserWithAr < ActiveRecord::Base
self.table_name = "users"
self.table_name = 'users'

validates :homepage, :url => true
validates :homepage, url: true
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_ar_legacy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserWithArLegacy < ActiveRecord::Base
self.table_name = "users"
self.table_name = 'users'

validates_url :homepage
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithBlank

attr_accessor :homepage

validates :homepage, :url => {:allow_blank => true}
validates :homepage, url: { allow_blank: true }
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_custom_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class UserWithCustomMessage

attr_accessor :homepage

validates :homepage, :url => {message: "wrong"}
validates :homepage, url: { message: 'wrong' }
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_custom_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class UserWithCustomScheme

attr_accessor :homepage

validates :homepage, :url => { :schemes => ['ftp'] }
validates :homepage, url: { schemes: ['ftp'] }
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_legacy_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithLegacySyntax

attr_accessor :homepage

validates_url :homepage, :allow_blank => true
validates_url :homepage, allow_blank: true
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_nil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithNil

attr_accessor :homepage

validates :homepage, :url => {:allow_nil => true}
validates :homepage, url: { allow_nil: true }
end
4 changes: 2 additions & 2 deletions spec/resources/user_with_no_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithNoLocal

attr_accessor :homepage

validates :homepage, :url => {:no_local => true}
end
validates :homepage, url: { no_local: true }
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_no_pre_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithNoPreQuery

attr_accessor :homepage

validates :homepage, :url => {:no_pre_query => true}
validates :homepage, url: { no_pre_query: true }
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_public_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class UserWithPublicSuffix

attr_accessor :homepage

validates :homepage, :url => {:public_suffix => true}
validates :homepage, url: { public_suffix: true }
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

ActiveRecord::Migration.verbose = false
ActiveRecord::Base.establish_connection(
"adapter" => "sqlite3",
"database" => ":memory:"
'adapter' => 'sqlite3',
'database' => ':memory:'
)

require File.join(File.dirname(__FILE__), '..', 'init')
Expand Down
Loading
Loading