validate_url 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +0 -0
- data/install.rb +1 -0
- data/lib/validates_url.rb +82 -0
- data/rails/init.rb +1 -0
- data/spec/resources/user.rb +9 -0
- data/spec/resources/user_with_blank.rb +9 -0
- data/spec/resources/user_with_legacy_syntax.rb +9 -0
- data/spec/resources/user_with_nil.rb +9 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/validates_url_spec.rb +105 -0
- metadata +108 -0
data/README.markdown
ADDED
File without changes
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
puts IO.read(File.join(File.dirname(__FILE__), 'README.markdown'))
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Perfectline
|
2
|
+
module ValidatesUrl
|
3
|
+
|
4
|
+
class Validator
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
def self.valid_url?(value, schemes)
|
8
|
+
schemes = [*schemes].map(&:to_s)
|
9
|
+
begin
|
10
|
+
uri = ::URI.parse(value)
|
11
|
+
|
12
|
+
unless schemes.include?(uri.scheme)
|
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
|
37
|
+
options.reverse_merge!(:schemes => %w(http https))
|
38
|
+
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
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module Rails3
|
49
|
+
require 'active_model'
|
50
|
+
require 'active_model/validations'
|
51
|
+
require 'active_support/concern'
|
52
|
+
|
53
|
+
extend ActiveSupport::Concern
|
54
|
+
|
55
|
+
class UrlValidator < ActiveModel::EachValidator
|
56
|
+
def initialize(options)
|
57
|
+
options.reverse_merge!(:schemes => %w(http https))
|
58
|
+
options.reverse_merge!(:message => "is not a valid URL")
|
59
|
+
super(options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def validate_each(record, attribute, value)
|
63
|
+
unless Perfectline::ValidatesUrl::Validator.valid_url?(value, options.fetch(:schemes))
|
64
|
+
record.errors.add(attribute, options.fetch(:message), :value => value)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module ClassMethods
|
70
|
+
def validates_url(*attr_names)
|
71
|
+
validates_with UrlValidator, _merge_attributes(attr_names)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
if Rails::VERSION::MAJOR >= 3
|
79
|
+
ActiveModel::Validations.send(:include, Perfectline::ValidatesUrl::Rails3)
|
80
|
+
else
|
81
|
+
ActiveRecord::Base.send(:include, Perfectline::ValidatesUrl::Rails2)
|
82
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_url'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/resources')
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
require 'spec'
|
6
|
+
require 'rails'
|
7
|
+
require 'validates_url'
|
8
|
+
|
9
|
+
autoload :User, 'resources/user'
|
10
|
+
autoload :UserWithNil, 'resources/user_with_nil'
|
11
|
+
autoload :UserWithBlank, 'resources/user_with_blank'
|
12
|
+
autoload :UserWithLegacySyntax, 'resources/user_with_legacy_syntax'
|
13
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "URL validation", Perfectline::ValidatesUrl::Rails3::UrlValidator do
|
4
|
+
|
5
|
+
context "with regular validator" do
|
6
|
+
before do
|
7
|
+
@user = User.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not allow nil as url" do
|
11
|
+
@user.homepage = nil
|
12
|
+
@user.should_not be_valid
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not allow blank as url" do
|
16
|
+
@user.homepage = ""
|
17
|
+
@user.should_not be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not allow an url without scheme" do
|
21
|
+
@user.homepage = "www.example.com"
|
22
|
+
@user.should_not be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should allow an url with http" do
|
26
|
+
@user.homepage = "http://localhost"
|
27
|
+
@user.should be_valid
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow an url with https" do
|
31
|
+
@user.homepage = "https://localhost"
|
32
|
+
@user.should be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with allow nil" do
|
38
|
+
before do
|
39
|
+
@user = UserWithNil.new
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should allow nil as url" do
|
43
|
+
@user.homepage = nil
|
44
|
+
@user.should be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should not allow blank as url" do
|
48
|
+
@user.homepage = ""
|
49
|
+
@user.should_not be_valid
|
50
|
+
end
|
51
|
+
|
52
|
+
it "shoild allow a valid url" do
|
53
|
+
@user.homepage = "http://www.example.com"
|
54
|
+
@user.should be_valid
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with allow blank" do
|
59
|
+
before do
|
60
|
+
@user = UserWithBlank.new
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should allow nil as url" do
|
64
|
+
@user.homepage = nil
|
65
|
+
@user.should be_valid
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should allow blank as url" do
|
69
|
+
@user.homepage = ""
|
70
|
+
@user.should be_valid
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should allow a valid url" do
|
74
|
+
@user.homepage = "http://www.example.com"
|
75
|
+
@user.should be_valid
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "with legacy syntax" do
|
80
|
+
before do
|
81
|
+
@user = UserWithLegacySyntax.new
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should allow nil as url" do
|
85
|
+
@user.homepage = nil
|
86
|
+
@user.should be_valid
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should allow blank as url" do
|
90
|
+
@user.homepage = ""
|
91
|
+
@user.should be_valid
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should allow a valid url" do
|
95
|
+
@user.homepage = "http://www.example.com"
|
96
|
+
@user.should be_valid
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should not allow invalid url" do
|
100
|
+
@user.homepage = "random"
|
101
|
+
@user.should_not be_valid
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tanel Suurhans
|
13
|
+
- Tarmo Lehtpuu
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-08 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: diff-lcs
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 1
|
45
|
+
- 2
|
46
|
+
version: 1.1.2
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Library for validating urls in Rails.
|
50
|
+
email:
|
51
|
+
- tanel.suurhans@perfectline.ee
|
52
|
+
- tarmo.lehtpuu@perfectline.ee
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
58
|
+
- README.markdown
|
59
|
+
files:
|
60
|
+
- README.markdown
|
61
|
+
- install.rb
|
62
|
+
- lib/validates_url.rb
|
63
|
+
- rails/init.rb
|
64
|
+
- spec/resources/user.rb
|
65
|
+
- spec/resources/user_with_blank.rb
|
66
|
+
- spec/resources/user_with_legacy_syntax.rb
|
67
|
+
- spec/resources/user_with_nil.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
- spec/validates_url_spec.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/perfectline/validates_url/tree/master
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Library for validating urls in Rails.
|
102
|
+
test_files:
|
103
|
+
- spec/resources/user.rb
|
104
|
+
- spec/resources/user_with_blank.rb
|
105
|
+
- spec/resources/user_with_legacy_syntax.rb
|
106
|
+
- spec/resources/user_with_nil.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
- spec/validates_url_spec.rb
|