can_has_validations 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/lib/can_has_validations/validators/url_validator.rb +43 -10
- data/lib/can_has_validations/version.rb +1 -1
- data/test/dummy/log/test.log +17 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb51b71bb23aabfeb3e1bde9ad0f03119d4011d60c7117971c606266eb0c477a
|
4
|
+
data.tar.gz: 5b101d95e237b874ff096f9658c064709aef19228c3444f6ed6878fb1f5cc1c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82e3dc6177df9d32216bbefbbfeef97ccb4bebf22d5b8431c9d59393fd619f2d465b52f3c26eb84c551f35c154279e54c5689d595e699de0b65d211bc115c0ef
|
7
|
+
data.tar.gz: fe709a9a7225cf2ea6eb7828c4d741b11c07181a32710d394972ddb424bc282d44d9f163388451671577ff0ebf96c28b3921f46d60b63e61f8be2384a0702e08
|
data/MIT-LICENSE
CHANGED
@@ -3,26 +3,59 @@
|
|
3
3
|
# eg: validates :website, url: true
|
4
4
|
# validates :redis, url: {scheme: 'redis'}
|
5
5
|
# validates :database, url: {scheme: %w(postgres mysql)}
|
6
|
+
# validates :website, url: {host: 'example.com'}
|
7
|
+
# validates :database, url: {port: [5432, nil]}
|
8
|
+
# to allow scheme's default port, must specify `nil` too
|
9
|
+
# :scheme defaults to `%w(http https)`
|
10
|
+
# :host defaults to `nil` which allows any
|
11
|
+
# :port defaults to `nil` which allows any
|
12
|
+
# to require blank, use `port: false` or `port: [nil]`
|
6
13
|
|
7
14
|
module ActiveModel::Validations
|
8
15
|
class UrlValidator < ActiveModel::EachValidator
|
9
16
|
def validate_each(record, attribute, value)
|
10
|
-
allowed_schemes = if options[:scheme].respond_to?(:call)
|
11
|
-
options[:scheme].call(record)
|
12
|
-
elsif options[:scheme].is_a?(Symbol)
|
13
|
-
record.send(options[:scheme])
|
14
|
-
else
|
15
|
-
Array.wrap(options[:scheme] || %w(http https))
|
16
|
-
end
|
17
|
-
|
18
17
|
if defined?(Addressable::URI)
|
19
18
|
u = Addressable::URI.parse(value) rescue nil
|
20
19
|
u2 = u && URI.parse(u.normalize.to_s) rescue nil
|
21
20
|
else
|
22
21
|
u2 = u = URI.parse(value) rescue nil
|
23
22
|
end
|
24
|
-
|
25
|
-
|
23
|
+
|
24
|
+
allowed_schemes =
|
25
|
+
if options[:scheme].respond_to?(:call)
|
26
|
+
options[:scheme].call(record)
|
27
|
+
elsif options[:scheme].is_a?(Symbol)
|
28
|
+
record.send(options[:scheme])
|
29
|
+
else
|
30
|
+
Array.wrap(options[:scheme] || %w(http https))
|
31
|
+
end
|
32
|
+
|
33
|
+
allowed_hosts =
|
34
|
+
if options[:host].respond_to?(:call)
|
35
|
+
options[:host].call(record)
|
36
|
+
elsif options[:host].is_a?(Symbol)
|
37
|
+
record.send(options[:host])
|
38
|
+
elsif options[:host].nil?
|
39
|
+
[u&.host]
|
40
|
+
else
|
41
|
+
Array.wrap(options[:host])
|
42
|
+
end
|
43
|
+
|
44
|
+
allowed_ports =
|
45
|
+
if options[:port].respond_to?(:call)
|
46
|
+
options[:port].call(record)
|
47
|
+
elsif options[:port].is_a?(Symbol)
|
48
|
+
record.send(options[:port])
|
49
|
+
elsif options[:port].nil?
|
50
|
+
[u&.port]
|
51
|
+
elsif options[:port] == false
|
52
|
+
[nil]
|
53
|
+
else
|
54
|
+
Array.wrap(options[:port])
|
55
|
+
end
|
56
|
+
|
57
|
+
if !u || !u2 || u.relative? || allowed_schemes.exclude?(u.scheme) || allowed_hosts.exclude?(u.host) || allowed_ports.exclude?(u.port)
|
58
|
+
record.errors.add(attribute, :invalid_url, **options.merge(value: value, scheme: allowed_schemes, host: allowed_hosts, port: allowed_ports))
|
26
59
|
end
|
27
60
|
end
|
28
61
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -2,5 +2,22 @@
|
|
2
2
|
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
3
3
|
---------------------------------
|
4
4
|
CanHasValidationsTest: test_truth
|
5
|
+
---------------------------------
|
6
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
7
|
+
[1m[35m (0.8ms)[0m [1m[34mSELECT sqlite_version(*)[0m
|
8
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
9
|
+
---------------------------------
|
10
|
+
CanHasValidationsTest: test_truth
|
11
|
+
---------------------------------
|
12
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
13
|
+
[1m[35m (0.6ms)[0m [1m[34mSELECT sqlite_version(*)[0m
|
14
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
15
|
+
---------------------------------
|
16
|
+
CanHasValidationsTest: test_truth
|
17
|
+
---------------------------------
|
18
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
19
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
20
|
+
---------------------------------
|
21
|
+
CanHasValidationsTest: test_truth
|
5
22
|
---------------------------------
|
6
23
|
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
- !ruby/object:Gem::Version
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
|
-
rubygems_version: 3.
|
150
|
+
rubygems_version: 3.3.26
|
151
151
|
signing_key:
|
152
152
|
specification_version: 4
|
153
153
|
summary: Assorted Rails 5.x-7.x validators
|