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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 157fb70e3a605aec6febb5363358ca5e46f5e05b8c76875168eeb6c5668aa1f4
4
- data.tar.gz: 7d6dcbfa72be63a24ae0a96ee185bdc95b31ef30ca01dc028c536e11b3123468
3
+ metadata.gz: 58b59a8faf6055485804d8dbc80aa6f70cec016dabf65eb0fa79d0cb9d3c6db5
4
+ data.tar.gz: b9015820dc18729bab6cbe3e67d982918e7727ad611d7ab916e854dbf39b7a74
5
5
  SHA512:
6
- metadata.gz: ed1f29124a7b78f2d0523e177ee28eac5e2d7295f1123fd10d230cbfdbcff42f48f1ab00691a7d646f889ee6954cd089b562ab20adb837ec617702af8bfffb53
7
- data.tar.gz: d1435794c3ad4e8f080ee1f40ebf4fdd86e50e8a6fd7d3341a2b04ab2b856d09eb8b41fb7cdda3c9d89bfbfd75ee96fbe05d6fa4f9789038eb554f34a9e3fc5f
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
- # add this to your Gemfile
7
+ Add this to your `Gemfile`:
8
+
9
+ ```ruby
9
10
  gem "validate_url"
11
+ ```
12
+
13
+ Or install it yourself:
10
14
 
11
- # or run
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
- Fix validate_url.gemspec to remove unneeded wrong strings
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
@@ -1,4 +1,4 @@
1
- en:
1
+ ar:
2
2
  errors:
3
3
  messages:
4
4
  url: عنوان الموقع غير صالح
data/lib/locale/it.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  it:
2
2
  errors:
3
3
  messages:
4
- url: non è un URL valido.
4
+ url: non è un URL valido
data/lib/locale/nl.yml ADDED
@@ -0,0 +1,4 @@
1
+ nl:
2
+ errors:
3
+ messages:
4
+ url: is geen geldige URL
data/lib/locale/pl.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  pl:
2
2
  errors:
3
3
  messages:
4
- url: nie jest poprawnym adresem URL.
4
+ url: nie jest poprawnym adresem URL
@@ -0,0 +1,4 @@
1
+ pt-PT:
2
+ errors:
3
+ messages:
4
+ url: não é uma URL válida
data/lib/locale/pt.yml ADDED
@@ -0,0 +1,4 @@
1
+ pt:
2
+ errors:
3
+ messages:
4
+ url: não é uma URL válida
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
- begin
23
- uri = URI.parse(value)
24
- host = uri && uri.host
25
- scheme = uri && uri.scheme
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
- valid_raw_url = scheme && value =~ /\A#{URI::regexp([scheme])}\z/
28
- valid_scheme = host && scheme && schemes.include?(scheme)
29
- valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
30
- valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, :default_rule => nil))
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
- unless valid_raw_url && valid_scheme && valid_no_local && valid_suffix
33
- record.errors.add(attribute, options.fetch(:message), value: value)
34
- end
35
- rescue URI::InvalidURIError
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.12
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: 2020-09-22 00:00:00.000000000 Z
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