activemodel-url_validator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/url_validator.rb +26 -5
- data/lib/url_validator/version.rb +1 -1
- data/spec/url_validator_spec.rb +36 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bae84fcd1d6d45196a3c26227939face9d9cfe6
|
4
|
+
data.tar.gz: 92eb6f02caa3c8a11297a5d2cefa4c5caaa97262
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4166f3b5878a7c02736e8cd776753811a938f883b468715e783167169d31b007f11d621ffdedeac681a0c642db252707faee6156b03ed8b9496bc25d91f84b45
|
7
|
+
data.tar.gz: 07e8f27dc5a60b58c37e26e1efeb0fa3c5111302ab612af9e04663c28ee8562b3102c254a93cb9d243baf2394b3bcf606d88cff19502dba0cdca2199f1a86538
|
data/README.md
CHANGED
@@ -28,6 +28,7 @@ Name | Value | Default | Desc.
|
|
28
28
|
----|----|----|----
|
29
29
|
`scheme` | Array of String | nil | Specify allowed scheme types.
|
30
30
|
`allow_no_scheme` | Boolean | false | Whether scheme less URI is allowed.
|
31
|
+
`allow_no_host` | Boolean | false | Whether host less URI is allowed.
|
31
32
|
|
32
33
|
```rb
|
33
34
|
validates :my_url_attribute, url: { scheme: ['https'] }
|
data/lib/url_validator.rb
CHANGED
@@ -3,17 +3,38 @@ class UrlValidator < ActiveModel::EachValidator
|
|
3
3
|
def valid?(value, options = {})
|
4
4
|
options = default_options.merge(options)
|
5
5
|
uri = URI.parse(value)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
options[:scheme] ? options[:scheme].include?(uri.scheme) : true
|
6
|
+
|
7
|
+
[:scheme_appearance, :host_appearance, :scheme_type].all? do |method|
|
8
|
+
send("validate_#{method}", uri, options)
|
10
9
|
end
|
11
10
|
rescue URI::Error
|
12
11
|
false
|
13
12
|
end
|
14
13
|
|
15
14
|
def default_options
|
16
|
-
{ scheme: nil, allow_no_scheme: false }
|
15
|
+
{ scheme: nil, allow_no_scheme: false, allow_no_host: false }
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def validate_scheme_appearance(uri, options)
|
21
|
+
uri.scheme.nil? ? options[:allow_no_scheme] : true
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_host_appearance(uri, options)
|
25
|
+
uri.host.nil? ? options[:allow_no_host] : true
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_scheme_type(uri, options)
|
29
|
+
if options[:scheme]
|
30
|
+
if uri.scheme.nil? && options[:allow_no_scheme]
|
31
|
+
true
|
32
|
+
else
|
33
|
+
options[:scheme].include?(uri.scheme)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
true
|
37
|
+
end
|
17
38
|
end
|
18
39
|
end
|
19
40
|
|
data/spec/url_validator_spec.rb
CHANGED
@@ -14,6 +14,12 @@ shared_examples_for 'scheme less value is given', given: :scheme_less do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
shared_examples_for 'host less value is given', given: :host_less do
|
18
|
+
let(:value) do
|
19
|
+
'http:///host/less'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
17
23
|
[:http, :https].each do |scheme|
|
18
24
|
shared_examples_for "#{scheme} URI is given", given: scheme do
|
19
25
|
let(:value) do
|
@@ -30,6 +36,12 @@ shared_examples_for 'allow_no_scheme is true', allow_no_scheme: true do
|
|
30
36
|
end
|
31
37
|
end
|
32
38
|
|
39
|
+
shared_examples_for 'allow_no_host is true', allow_no_host: true do
|
40
|
+
let(:allow_no_host) do
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
33
45
|
shared_examples_for 'scheme includes http', scheme: :http do
|
34
46
|
let(:scheme) do
|
35
47
|
['http']
|
@@ -72,6 +84,14 @@ describe UrlValidator do
|
|
72
84
|
end
|
73
85
|
end
|
74
86
|
|
87
|
+
context 'when a host less URI is given', given: :host_less do
|
88
|
+
it { should be_falsey }
|
89
|
+
|
90
|
+
context 'and host less URI is allowed', allow_no_host: true do
|
91
|
+
it { should be_truthy }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
75
95
|
context 'when a HTTP URI is given', given: :http do
|
76
96
|
it { should be_truthy }
|
77
97
|
|
@@ -109,6 +129,10 @@ describe UrlValidator do
|
|
109
129
|
it { should be_invalid }
|
110
130
|
end
|
111
131
|
|
132
|
+
context 'and a valid host less value is given', given: :host_less do
|
133
|
+
it { should be_invalid }
|
134
|
+
end
|
135
|
+
|
112
136
|
context 'and a valid http value is given', given: :http do
|
113
137
|
it { should be_valid }
|
114
138
|
end
|
@@ -130,6 +154,18 @@ describe UrlValidator do
|
|
130
154
|
end
|
131
155
|
end
|
132
156
|
|
157
|
+
context 'when allow_no_host is true' do
|
158
|
+
let(:model_class) do
|
159
|
+
Class.new(TestModel) do
|
160
|
+
validates :attr, url: { allow_no_host: true }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context 'and a valid host less value is given', given: :host_less do
|
165
|
+
it { should be_valid }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
133
169
|
context 'when http scheme is allowed', scheme: :http do
|
134
170
|
let(:model_class) do
|
135
171
|
Class.new(TestModel) do
|