validate_url 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/validates_url.rb +15 -66
- data/spec/resources/user.rb +0 -2
- data/spec/resources/user_with_ar.rb +5 -0
- data/spec/resources/user_with_ar_legacy.rb +5 -0
- data/spec/spec_helper.rb +15 -4
- data/spec/validates_url_spec.rb +41 -1
- metadata +21 -6
data/lib/validates_url.rb
CHANGED
@@ -1,80 +1,29 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'uri'
|
2
|
+
require 'active_model'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def self.valid_url?(value, schemes)
|
8
|
-
schemes = [*schemes].map(&:to_s)
|
9
|
-
begin
|
10
|
-
uri = ::URI.parse(value)
|
4
|
+
module ActiveModel
|
5
|
+
module Validations
|
6
|
+
class UrlValidator < ActiveModel::EachValidator
|
11
7
|
|
12
|
-
|
13
|
-
raise ::URI::InvalidURIError
|
14
|
-
end
|
15
|
-
|
16
|
-
unless uri.send(:scheme).present?
|
17
|
-
raise ::URI::InvalidURIError
|
18
|
-
end
|
19
|
-
|
20
|
-
unless uri.send(:host).present?
|
21
|
-
raise ::URI::InvalidURIError
|
22
|
-
end
|
23
|
-
|
24
|
-
rescue ::URI::InvalidURIError
|
25
|
-
return false
|
26
|
-
end
|
27
|
-
|
28
|
-
true
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
module Rails2
|
33
|
-
require 'active_record'
|
34
|
-
|
35
|
-
def validates_url(*attribute_names)
|
36
|
-
options = attribute_names.extract_options!.symbolize_keys
|
8
|
+
def initialize(options)
|
37
9
|
options.reverse_merge!(:schemes => %w(http https))
|
38
10
|
options.reverse_merge!(:message => "is not a valid URL")
|
39
|
-
|
40
|
-
validates_each(attribute_names, options) do |record, attribute, value|
|
41
|
-
unless valid_url?(value, options.fetch(:schemes))
|
42
|
-
record.errors.add(attribute, options[:message], :value => value)
|
43
|
-
end
|
44
|
-
end
|
11
|
+
super(options)
|
45
12
|
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module Rails3
|
49
|
-
require 'active_model'
|
50
|
-
require 'active_model/validations'
|
51
13
|
|
52
|
-
|
53
|
-
|
54
|
-
options.reverse_merge!(:schemes => %w(http https))
|
55
|
-
options.reverse_merge!(:message => "is not a valid URL")
|
56
|
-
super(options)
|
57
|
-
end
|
14
|
+
def validate_each(record, attribute, value)
|
15
|
+
schemes = [*options.fetch(:schemes)].map(&:to_s)
|
58
16
|
|
59
|
-
|
60
|
-
|
61
|
-
record.errors.add(attribute, options.fetch(:message), :value => value)
|
62
|
-
end
|
17
|
+
unless URI::regexp(schemes).match(value)
|
18
|
+
record.errors.add(attribute, options.fetch(:message), :value => value)
|
63
19
|
end
|
64
20
|
end
|
21
|
+
end
|
65
22
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
23
|
+
module ClassMethods
|
24
|
+
def validates_url(*attr_names)
|
25
|
+
validates_with UrlValidator, _merge_attributes(attr_names)
|
70
26
|
end
|
71
27
|
end
|
72
28
|
end
|
73
|
-
end
|
74
|
-
|
75
|
-
if Rails::VERSION::MAJOR >= 3
|
76
|
-
ActiveModel::Validations::UrlValidator = Perfectline::ValidatesUrl::Rails3::UrlValidator
|
77
|
-
ActiveModel::Validations::HelperMethods.send(:include, Perfectline::ValidatesUrl::Rails3::ClassMethods)
|
78
|
-
else
|
79
|
-
ActiveRecord::Base.send(:include, Perfectline::ValidatesUrl::Rails2)
|
80
29
|
end
|
data/spec/resources/user.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -2,12 +2,23 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/resources')
|
3
3
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
4
|
|
5
|
-
require '
|
6
|
-
require '
|
7
|
-
require '
|
5
|
+
require 'rspec'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'active_record'
|
8
|
+
require 'active_record/base'
|
9
|
+
require 'active_record/migration'
|
10
|
+
|
11
|
+
ActiveRecord::Migration.verbose = false
|
12
|
+
ActiveRecord::Base.establish_connection(
|
13
|
+
"adapter" => "sqlite3",
|
14
|
+
"database" => ":memory:"
|
15
|
+
)
|
16
|
+
|
17
|
+
require File.join(File.dirname(__FILE__), '..', 'rails', 'init')
|
8
18
|
|
9
19
|
autoload :User, 'resources/user'
|
10
20
|
autoload :UserWithNil, 'resources/user_with_nil'
|
11
21
|
autoload :UserWithBlank, 'resources/user_with_blank'
|
12
22
|
autoload :UserWithLegacySyntax, 'resources/user_with_legacy_syntax'
|
13
|
-
|
23
|
+
autoload :UserWithAr, 'resources/user_with_ar'
|
24
|
+
autoload :UserWithArLegacy, 'resources/user_with_ar_legacy'
|
data/spec/validates_url_spec.rb
CHANGED
@@ -1,6 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "URL validation"
|
3
|
+
describe "URL validation" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
ActiveRecord::Schema.define(:version => 1) do
|
7
|
+
|
8
|
+
create_table :users, :force => true do |t|
|
9
|
+
t.column :homepage, :string
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) do
|
16
|
+
ActiveRecord::Base.connection.drop_table(:users)
|
17
|
+
end
|
4
18
|
|
5
19
|
context "with regular validator" do
|
6
20
|
before do
|
@@ -32,6 +46,10 @@ describe "URL validation", Perfectline::ValidatesUrl::Rails3::UrlValidator do
|
|
32
46
|
@user.should be_valid
|
33
47
|
end
|
34
48
|
|
49
|
+
it "shout not allow a url with an invalid scheme" do
|
50
|
+
@user.homepage = "ftp://localhost"
|
51
|
+
@user.should_not be_valid
|
52
|
+
end
|
35
53
|
end
|
36
54
|
|
37
55
|
context "with allow nil" do
|
@@ -102,4 +120,26 @@ describe "URL validation", Perfectline::ValidatesUrl::Rails3::UrlValidator do
|
|
102
120
|
end
|
103
121
|
end
|
104
122
|
|
123
|
+
context "with ActiveRecord" do
|
124
|
+
before do
|
125
|
+
@user = UserWithAr.new
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should not allow invalid url" do
|
129
|
+
@user.homepage = "random"
|
130
|
+
@user.should_not be_valid
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "with ActiveRecord and legacy syntax" do
|
135
|
+
before do
|
136
|
+
@user = UserWithArLegacy.new
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not allow invalid url" do
|
140
|
+
@user.homepage = "random"
|
141
|
+
@user.should_not be_valid
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
105
145
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tanel Suurhans
|
@@ -22,6 +22,7 @@ dependencies:
|
|
22
22
|
name: activemodel
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
@@ -36,6 +37,7 @@ dependencies:
|
|
36
37
|
name: rspec
|
37
38
|
prerelease: false
|
38
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
39
41
|
requirements:
|
40
42
|
- - ">="
|
41
43
|
- !ruby/object:Gem::Version
|
@@ -48,6 +50,7 @@ dependencies:
|
|
48
50
|
name: diff-lcs
|
49
51
|
prerelease: false
|
50
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
51
54
|
requirements:
|
52
55
|
- - ">="
|
53
56
|
- !ruby/object:Gem::Version
|
@@ -73,6 +76,14 @@ files:
|
|
73
76
|
- install.rb
|
74
77
|
- lib/validates_url.rb
|
75
78
|
- rails/init.rb
|
79
|
+
- spec/validates_url_spec.rb
|
80
|
+
- spec/resources/user_with_blank.rb
|
81
|
+
- spec/resources/user_with_nil.rb
|
82
|
+
- spec/resources/user_with_ar.rb
|
83
|
+
- spec/resources/user_with_legacy_syntax.rb
|
84
|
+
- spec/resources/user_with_ar_legacy.rb
|
85
|
+
- spec/resources/user.rb
|
86
|
+
- spec/spec_helper.rb
|
76
87
|
has_rdoc: true
|
77
88
|
homepage: http://github.com/perfectline/validates_url/tree/master
|
78
89
|
licenses: []
|
@@ -83,6 +94,7 @@ rdoc_options:
|
|
83
94
|
require_paths:
|
84
95
|
- lib
|
85
96
|
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
86
98
|
requirements:
|
87
99
|
- - ">="
|
88
100
|
- !ruby/object:Gem::Version
|
@@ -90,6 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
102
|
- 0
|
91
103
|
version: "0"
|
92
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
93
106
|
requirements:
|
94
107
|
- - ">="
|
95
108
|
- !ruby/object:Gem::Version
|
@@ -99,14 +112,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
112
|
requirements: []
|
100
113
|
|
101
114
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.3.
|
115
|
+
rubygems_version: 1.3.7
|
103
116
|
signing_key:
|
104
117
|
specification_version: 3
|
105
118
|
summary: Library for validating urls in Rails.
|
106
119
|
test_files:
|
107
|
-
- spec/
|
120
|
+
- spec/validates_url_spec.rb
|
108
121
|
- spec/resources/user_with_blank.rb
|
109
|
-
- spec/resources/user_with_legacy_syntax.rb
|
110
122
|
- spec/resources/user_with_nil.rb
|
123
|
+
- spec/resources/user_with_ar.rb
|
124
|
+
- spec/resources/user_with_legacy_syntax.rb
|
125
|
+
- spec/resources/user_with_ar_legacy.rb
|
126
|
+
- spec/resources/user.rb
|
111
127
|
- spec/spec_helper.rb
|
112
|
-
- spec/validates_url_spec.rb
|