valid8ors 0.0.2 → 0.0.3
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/lib/valid8ors/url_format.rb +26 -0
- data/lib/valid8ors.rb +1 -0
- data/test/url_format_test.rb +93 -0
- data/valid8ors.gemspec +3 -3
- metadata +9 -4
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
class UrlFormatValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
URL_PATTERN = /^(http|https):\/\/[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}((:[0-9]{1,5})?\/.*)?$/
|
6
|
+
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
unless value =~ URL_PATTERN
|
9
|
+
record.errors.add(attribute, options[:message] || invalid_message(record, attribute))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
#######################
|
14
|
+
### Private methods ###
|
15
|
+
#######################
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def invalid_message(record, attribute)
|
20
|
+
I18n.t :improperly_formatted,
|
21
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
22
|
+
default: "is improperly formatted"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
data/lib/valid8ors.rb
CHANGED
@@ -0,0 +1,93 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'test_helper'
|
4
|
+
|
5
|
+
class TestSite < TestModel
|
6
|
+
validates :url, url_format: true
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestSiteAllowsNilToTrue < TestModel
|
10
|
+
validates :url, url_format: { allow_nil: true }
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestSiteAllowsNilToFalse < TestModel
|
14
|
+
validates :url, url_format: { allow_nil: false }
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestSiteWithMessage < TestModel
|
18
|
+
validates :url, url_format: { message: 'is not well formatted' }
|
19
|
+
end
|
20
|
+
|
21
|
+
class TestUrlFormatValidator < MiniTest::Unit::TestCase
|
22
|
+
|
23
|
+
def test_valid_url
|
24
|
+
valid_urls.each { |url| assert TestSite.new(url: url).valid? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_invalid_url
|
28
|
+
invalid_urls.each { |url| refute TestSite.new(url: url).valid? }
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_default_message_on_error
|
32
|
+
test_site = TestSite.new(url: "invalid_url")
|
33
|
+
refute test_site.valid?
|
34
|
+
assert test_site.errors[:url].include?("is improperly formatted")
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_custom_message_on_error
|
38
|
+
test_site = TestSiteWithMessage.new(url: "invalid_url")
|
39
|
+
refute test_site.valid?
|
40
|
+
assert test_site.errors[:url].include?("is not well formatted")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_nil_url_when_allow_nil_option_is_not_set
|
44
|
+
refute TestSite.new(url: nil).valid?
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_nil_url_when_allow_nil_option_is_set_to_true
|
48
|
+
assert TestSiteAllowsNilToTrue.new(url: nil).valid?
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_nil_url_when_allow_nil_option_is_set_to_false
|
52
|
+
refute TestSiteAllowsNilToFalse.new(url: nil).valid?
|
53
|
+
end
|
54
|
+
|
55
|
+
#######################
|
56
|
+
### Private methods ###
|
57
|
+
#######################
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def valid_urls
|
62
|
+
[
|
63
|
+
'http://test.com',
|
64
|
+
'http://www.test.ch',
|
65
|
+
'https://super.long.url.with.many.subdomaim.com',
|
66
|
+
'http://url.ch/resources',
|
67
|
+
'http://url.ch/many/resources/after_path',
|
68
|
+
'http://domain.com/?withparam=true',
|
69
|
+
'http://domain.com/?withparam=true&otherparam=true',
|
70
|
+
'http://domain.com/#with_anchor',
|
71
|
+
'http://domain.com/#with_anchor?and=params',
|
72
|
+
'http://domain.com/#with_anchor?and=param&other=params',
|
73
|
+
]
|
74
|
+
end
|
75
|
+
|
76
|
+
def invalid_urls
|
77
|
+
[
|
78
|
+
'',
|
79
|
+
'test',
|
80
|
+
'http://s.c',
|
81
|
+
'http://.ru',
|
82
|
+
'test.ca',
|
83
|
+
'http://test.ca.c',
|
84
|
+
'http://test?xsxa',
|
85
|
+
'bla://service.com',
|
86
|
+
'http://test.toolongdomain',
|
87
|
+
'http://énon.ch',
|
88
|
+
'http://do main.com/#with_anchor',
|
89
|
+
''
|
90
|
+
]
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
data/valid8ors.gemspec
CHANGED
@@ -3,9 +3,9 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "valid8ors"
|
6
|
-
s.version = "0.0.
|
7
|
-
s.authors = ["Axel Vergult"]
|
8
|
-
s.email = ["axel@official.fm"]
|
6
|
+
s.version = "0.0.3"
|
7
|
+
s.authors = ["Axel Vergult", "Vincent Pochet"]
|
8
|
+
s.email = ["axel@official.fm", "vincent@official.fm"]
|
9
9
|
s.homepage = ""
|
10
10
|
s.summary = %q{Rails 3 awesome custom validators}
|
11
11
|
s.description = %q{Rails 3 awesome custom validators}
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid8ors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Axel Vergult
|
9
|
+
- Vincent Pochet
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
+
date: 2011-12-20 00:00:00.000000000Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: activemodel
|
16
|
-
requirement: &
|
17
|
+
requirement: &70335761002560 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,10 +22,11 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
+
version_requirements: *70335761002560
|
25
26
|
description: Rails 3 awesome custom validators
|
26
27
|
email:
|
27
28
|
- axel@official.fm
|
29
|
+
- vincent@official.fm
|
28
30
|
executables: []
|
29
31
|
extensions: []
|
30
32
|
extra_rdoc_files: []
|
@@ -38,9 +40,11 @@ files:
|
|
38
40
|
- lib/valid8ors.rb
|
39
41
|
- lib/valid8ors/blacklist.rb
|
40
42
|
- lib/valid8ors/email_format.rb
|
43
|
+
- lib/valid8ors/url_format.rb
|
41
44
|
- test/blacklist_test.rb
|
42
45
|
- test/email_format_test.rb
|
43
46
|
- test/test_helper.rb
|
47
|
+
- test/url_format_test.rb
|
44
48
|
- valid8ors.gemspec
|
45
49
|
homepage: ''
|
46
50
|
licenses: []
|
@@ -70,3 +74,4 @@ test_files:
|
|
70
74
|
- test/blacklist_test.rb
|
71
75
|
- test/email_format_test.rb
|
72
76
|
- test/test_helper.rb
|
77
|
+
- test/url_format_test.rb
|