validate_url 1.0.12 → 1.0.15
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.
- checksums.yaml +4 -4
- data/README.md +17 -8
- data/lib/locale/ar.yml +1 -1
- data/lib/locale/it.yml +1 -1
- data/lib/locale/nl.yml +4 -0
- data/lib/locale/pl.yml +1 -1
- data/lib/locale/pt-PT.yml +4 -0
- data/lib/locale/pt.yml +4 -0
- data/lib/validate_url.rb +34 -13
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58b59a8faf6055485804d8dbc80aa6f70cec016dabf65eb0fa79d0cb9d3c6db5
|
4
|
+
data.tar.gz: b9015820dc18729bab6cbe3e67d982918e7727ad611d7ab916e854dbf39b7a74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a630fa8358388a40491e00d3275fa543076a8d4c00df65d18a1e5d70426e862245b5631125782c72eab76cdbbdcff18f857f5a047e04cb8ec7bac36ccb770983
|
7
|
+
data.tar.gz: '09bd75d874a0087033f3984646b44466ffd275c58b77128dac06b4f8a1fe8bd13ab277a9e1317769a34407174f524380313b462b75f95f9be6a35340f15bf753'
|
data/README.md
CHANGED
@@ -4,11 +4,15 @@ This gem adds the capability of validating URLs to ActiveRecord and ActiveModel.
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
Add this to your `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
9
10
|
gem "validate_url"
|
11
|
+
```
|
12
|
+
|
13
|
+
Or install it yourself:
|
10
14
|
|
11
|
-
|
15
|
+
```sh
|
12
16
|
sudo gem install validate_url
|
13
17
|
```
|
14
18
|
|
@@ -35,6 +39,9 @@ class Pony < ActiveRecord::Base
|
|
35
39
|
|
36
40
|
# with public suffix database https://publicsuffix.org/
|
37
41
|
validates :homepage, url: { public_suffix: true }
|
42
|
+
|
43
|
+
# with Postgres array of urls, described [here](https://guides.rubyonrails.org/active_record_postgresql.html#array)
|
44
|
+
validates :homepage, url: { accept_array: true }
|
38
45
|
end
|
39
46
|
```
|
40
47
|
|
@@ -62,7 +69,7 @@ require 'validate_url/rspec_matcher'
|
|
62
69
|
In your spec:
|
63
70
|
|
64
71
|
```ruby
|
65
|
-
describe Unicorn
|
72
|
+
RSpec.describe Unicorn
|
66
73
|
it { is_expected.to validate_url_of(:homepage) }
|
67
74
|
end
|
68
75
|
```
|
@@ -87,14 +94,16 @@ Validates URL is created and maintained by [PerfectLine](http://www.perfectline.
|
|
87
94
|
Validates URL is Copyright © 2010-2014 [PerfectLine](http://www.perfectline.co), LLC. It is free software, and may be
|
88
95
|
redistributed under the terms specified in the LICENSE file.
|
89
96
|
|
90
|
-
## How to push new version
|
97
|
+
## How to push a new version
|
91
98
|
|
92
|
-
```
|
99
|
+
```sh
|
93
100
|
rake version:bump:patch
|
94
101
|
rake gemspec
|
95
102
|
```
|
96
|
-
|
97
|
-
|
103
|
+
|
104
|
+
Manually update `validate_url.gemspec` to remove incorrect strings.
|
105
|
+
|
106
|
+
```sh
|
98
107
|
gem build validate_url.gemspec
|
99
108
|
gem push validate_url-1.0.8.gem
|
100
109
|
```
|
data/lib/locale/ar.yml
CHANGED
data/lib/locale/it.yml
CHANGED
data/lib/locale/nl.yml
ADDED
data/lib/locale/pl.yml
CHANGED
data/lib/locale/pt.yml
ADDED
data/lib/validate_url.rb
CHANGED
@@ -13,28 +13,32 @@ module ActiveModel
|
|
13
13
|
options.reverse_merge!(message: :url)
|
14
14
|
options.reverse_merge!(no_local: false)
|
15
15
|
options.reverse_merge!(public_suffix: false)
|
16
|
+
options.reverse_merge!(accept_array: false)
|
16
17
|
|
17
18
|
super(options)
|
18
19
|
end
|
19
20
|
|
20
21
|
def validate_each(record, attribute, value)
|
21
22
|
schemes = [*options.fetch(:schemes)].map(&:to_s)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
if value.respond_to?(:each)
|
24
|
+
# Error out if we're not allowing arrays
|
25
|
+
if !options.include?(:accept_array) || !options.fetch(:accept_array)
|
26
|
+
record.errors.add(attribute, :url, **filtered_options(value))
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
# We have to manually handle `:allow_nil` and `:allow_blank` since it's not caught by
|
30
|
+
# ActiveRecord's own validators. We do that by just removing all the nil's if we want to
|
31
|
+
# allow them so it's not passed on later.
|
32
|
+
value = value.reject(&:nil?) if options.include?(:allow_nil) && options.fetch(:allow_nil)
|
33
|
+
value = value.reject(&:blank?) if options.include?(:allow_blank) && options.fetch(:allow_blank)
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
record.errors.add(attribute, :url, **filtered_options(value))
|
35
|
+
result = value.flat_map { |v| validate_url(record, attribute, v, schemes) }
|
36
|
+
errors = result.reject(&:nil?)
|
37
|
+
|
38
|
+
return errors.any? ? errors.first : true
|
37
39
|
end
|
40
|
+
|
41
|
+
validate_url(record, attribute, value, schemes)
|
38
42
|
end
|
39
43
|
|
40
44
|
protected
|
@@ -44,6 +48,23 @@ module ActiveModel
|
|
44
48
|
filtered[:value] = value
|
45
49
|
filtered
|
46
50
|
end
|
51
|
+
|
52
|
+
def validate_url(record, attribute, value, schemes)
|
53
|
+
uri = URI.parse(value)
|
54
|
+
host = uri && uri.host
|
55
|
+
scheme = uri && uri.scheme
|
56
|
+
|
57
|
+
valid_raw_url = scheme && value =~ /\A#{URI::regexp([scheme])}\z/
|
58
|
+
valid_scheme = host && scheme && schemes.include?(scheme)
|
59
|
+
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
|
60
|
+
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, :default_rule => nil))
|
61
|
+
|
62
|
+
unless valid_raw_url && valid_scheme && valid_no_local && valid_suffix
|
63
|
+
record.errors.add(attribute, options.fetch(:message), value: value)
|
64
|
+
end
|
65
|
+
rescue URI::InvalidURIError, URI::InvalidComponentError
|
66
|
+
record.errors.add(attribute, :url, **filtered_options(value))
|
67
|
+
end
|
47
68
|
end
|
48
69
|
|
49
70
|
module ClassMethods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate_url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tanel Suurhans
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-05-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: jeweler
|
@@ -133,8 +133,11 @@ files:
|
|
133
133
|
- lib/locale/it.yml
|
134
134
|
- lib/locale/ja.yml
|
135
135
|
- lib/locale/km.yml
|
136
|
+
- lib/locale/nl.yml
|
136
137
|
- lib/locale/pl.yml
|
137
138
|
- lib/locale/pt-BR.yml
|
139
|
+
- lib/locale/pt-PT.yml
|
140
|
+
- lib/locale/pt.yml
|
138
141
|
- lib/locale/ro.yml
|
139
142
|
- lib/locale/ru.yml
|
140
143
|
- lib/locale/tr.yml
|