validate_url 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,8 +40,11 @@ You can pass the `:message => "my custom error"` option to your validation to de
40
40
 
41
41
  ## Authors
42
42
 
43
- **Tanel Suurhans** (<http://twitter.com/tanelsuurhans>)
43
+ **Tanel Suurhans** (<http://twitter.com/tanelsuurhans>)
44
44
  **Tarmo Lehtpuu** (<http://twitter.com/tarmolehtpuu>)
45
45
 
46
+ ## Contributions
47
+ Thanks to **Steve Smith** for making the validation process even simpler.
48
+
46
49
  ## License
47
50
  Copyright 2010 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
@@ -14,13 +14,33 @@ module ActiveModel
14
14
  def validate_each(record, attribute, value)
15
15
  schemes = [*options.fetch(:schemes)].map(&:to_s)
16
16
 
17
- unless URI::regexp(schemes).match(value)
17
+ if URI::regexp(schemes).match(value)
18
+ begin
19
+ URI.parse(value)
20
+ rescue URI::InvalidURIError
21
+ record.errors.add(attribute, options.fetch(:message), :value => value)
22
+ end
23
+ else
18
24
  record.errors.add(attribute, options.fetch(:message), :value => value)
19
25
  end
20
26
  end
21
27
  end
22
28
 
23
29
  module ClassMethods
30
+ # Validates whether the value of the specified attribute is valid url.
31
+ #
32
+ # class Unicorn
33
+ # include ActiveModel::Validations
34
+ # attr_accessor :homepage, :ftpsite
35
+ # validates_url :homepage, :allow_blank => true
36
+ # validates_url :ftpsite, :schemes => ['ftp']
37
+ # end
38
+ # Configuration options:
39
+ # * <tt>:message</tt> - A custom error message (default is: "is not a valid URL").
40
+ # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
41
+ # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
42
+ # * <tt>:schemes</tt> - Array of URI schemes to validate against. (default is +['http', 'https']+)
43
+
24
44
  def validates_url(*attr_names)
25
45
  validates_with UrlValidator, _merge_attributes(attr_names)
26
46
  end
@@ -0,0 +1,7 @@
1
+ class UserWithCustomScheme
2
+ include ActiveModel::Validations
3
+
4
+ attr_accessor :homepage
5
+
6
+ validates :homepage, :url => { :schemes => ['ftp'] }
7
+ end
@@ -21,4 +21,5 @@ autoload :UserWithNil, 'resources/user_with_nil'
21
21
  autoload :UserWithBlank, 'resources/user_with_blank'
22
22
  autoload :UserWithLegacySyntax, 'resources/user_with_legacy_syntax'
23
23
  autoload :UserWithAr, 'resources/user_with_ar'
24
- autoload :UserWithArLegacy, 'resources/user_with_ar_legacy'
24
+ autoload :UserWithArLegacy, 'resources/user_with_ar_legacy'
25
+ autoload :UserWithCustomScheme, 'resources/user_with_custom_scheme'
@@ -46,10 +46,15 @@ describe "URL validation" do
46
46
  @user.should be_valid
47
47
  end
48
48
 
49
- it "shout not allow a url with an invalid scheme" do
49
+ it "should not allow a url with an invalid scheme" do
50
50
  @user.homepage = "ftp://localhost"
51
51
  @user.should_not be_valid
52
52
  end
53
+
54
+ it "should not allow a url with only a scheme" do
55
+ @user.homepage = "http://"
56
+ @user.should_not be_valid
57
+ end
53
58
  end
54
59
 
55
60
  context "with allow nil" do
@@ -67,7 +72,7 @@ describe "URL validation" do
67
72
  @user.should_not be_valid
68
73
  end
69
74
 
70
- it "shoild allow a valid url" do
75
+ it "should allow a valid url" do
71
76
  @user.homepage = "http://www.example.com"
72
77
  @user.should be_valid
73
78
  end
@@ -142,4 +147,14 @@ describe "URL validation" do
142
147
  end
143
148
  end
144
149
 
150
+ context "with regular validator and custom scheme" do
151
+ before do
152
+ @user = UserWithCustomScheme.new
153
+ end
154
+
155
+ it "should allow alternative URI schemes" do
156
+ @user.homepage = "ftp://ftp.example.com"
157
+ @user.should be_valid
158
+ end
159
+ end
145
160
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 6
9
- version: 0.1.6
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tanel Suurhans
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-16 00:00:00 +03:00
18
+ date: 2011-01-18 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -76,21 +76,22 @@ files:
76
76
  - init.rb
77
77
  - install.rb
78
78
  - lib/validate_url.rb
79
- - spec/validate_url_spec.rb
80
- - spec/resources/user_with_blank.rb
81
- - spec/resources/user_with_nil.rb
79
+ - spec/resources/user.rb
82
80
  - spec/resources/user_with_ar.rb
83
- - spec/resources/user_with_legacy_syntax.rb
84
81
  - spec/resources/user_with_ar_legacy.rb
85
- - spec/resources/user.rb
82
+ - spec/resources/user_with_blank.rb
83
+ - spec/resources/user_with_custom_scheme.rb
84
+ - spec/resources/user_with_legacy_syntax.rb
85
+ - spec/resources/user_with_nil.rb
86
86
  - spec/spec_helper.rb
87
+ - spec/validate_url_spec.rb
87
88
  has_rdoc: true
88
89
  homepage: http://github.com/perfectline/validates_url/tree/master
89
90
  licenses: []
90
91
 
91
92
  post_install_message:
92
- rdoc_options:
93
- - --charset=UTF-8
93
+ rdoc_options: []
94
+
94
95
  require_paths:
95
96
  - lib
96
97
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -117,11 +118,12 @@ signing_key:
117
118
  specification_version: 3
118
119
  summary: Library for validating urls in Rails.
119
120
  test_files:
120
- - spec/validate_url_spec.rb
121
- - spec/resources/user_with_blank.rb
122
- - spec/resources/user_with_nil.rb
121
+ - spec/resources/user.rb
123
122
  - spec/resources/user_with_ar.rb
124
- - spec/resources/user_with_legacy_syntax.rb
125
123
  - spec/resources/user_with_ar_legacy.rb
126
- - spec/resources/user.rb
124
+ - spec/resources/user_with_blank.rb
125
+ - spec/resources/user_with_custom_scheme.rb
126
+ - spec/resources/user_with_legacy_syntax.rb
127
+ - spec/resources/user_with_nil.rb
127
128
  - spec/spec_helper.rb
129
+ - spec/validate_url_spec.rb