validate_url 1.0.4 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9f28fe4da1f93c77418d9d1cd6ea529c7975a439
4
- data.tar.gz: c8495b475e179bd8d789739d025b2ff5d90aacbe
2
+ SHA256:
3
+ metadata.gz: 58b59a8faf6055485804d8dbc80aa6f70cec016dabf65eb0fa79d0cb9d3c6db5
4
+ data.tar.gz: b9015820dc18729bab6cbe3e67d982918e7727ad611d7ab916e854dbf39b7a74
5
5
  SHA512:
6
- metadata.gz: 38e4768c27ddb7685d8525b2ab3a4d92ac9dc5bde8d2e24c1db9dc1b8faea9a35729f098b26eb5b2c2e69f1db44ae1de8418756148e633a0956caea3b16a6c92
7
- data.tar.gz: 98b31de14eaa003131f459039c90258e4649b7f10d6a7b8754a5ff8b14524767da6ba77a85f55a5d12c9cb8dc994158dcf5a0ec6e8245472e2071ee6b1ca924b
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
- # and 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
 
@@ -51,6 +58,22 @@ class Unicorn
51
58
  end
52
59
  ```
53
60
 
61
+ ### With RSpec
62
+
63
+ Require the matcher in `spec_helper.rb` or `rails_helper.rb`:
64
+
65
+ ```ruby
66
+ require 'validate_url/rspec_matcher'
67
+ ```
68
+
69
+ In your spec:
70
+
71
+ ```ruby
72
+ RSpec.describe Unicorn
73
+ it { is_expected.to validate_url_of(:homepage) }
74
+ end
75
+ ```
76
+
54
77
  ### I18n
55
78
 
56
79
  The default error message `is not a valid URL`.
@@ -70,3 +93,17 @@ Validates URL is created and maintained by [PerfectLine](http://www.perfectline.
70
93
 
71
94
  Validates URL is Copyright © 2010-2014 [PerfectLine](http://www.perfectline.co), LLC. It is free software, and may be
72
95
  redistributed under the terms specified in the LICENSE file.
96
+
97
+ ## How to push a new version
98
+
99
+ ```sh
100
+ rake version:bump:patch
101
+ rake gemspec
102
+ ```
103
+
104
+ Manually update `validate_url.gemspec` to remove incorrect strings.
105
+
106
+ ```sh
107
+ gem build validate_url.gemspec
108
+ gem push validate_url-1.0.8.gem
109
+ ```
data/lib/locale/ar.yml ADDED
@@ -0,0 +1,4 @@
1
+ ar:
2
+ errors:
3
+ messages:
4
+ url: عنوان الموقع غير صالح
data/lib/locale/es.yml ADDED
@@ -0,0 +1,4 @@
1
+ es:
2
+ errors:
3
+ messages:
4
+ url: no es una URL válida
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/ja.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ja:
2
2
  errors:
3
3
  messages:
4
- url: は不正なURLです。
4
+ url: は不正なURLです
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 ADDED
@@ -0,0 +1,4 @@
1
+ pl:
2
+ errors:
3
+ messages:
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/locale/vi.yml ADDED
@@ -0,0 +1,4 @@
1
+ vi:
2
+ errors:
3
+ messages:
4
+ url: không phải là một địa chỉ liên kết hợp lệ
data/lib/validate_url.rb CHANGED
@@ -13,27 +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_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, :default_rule => nil))
28
- valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
29
- valid_scheme = host && scheme && schemes.include?(scheme)
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)
30
34
 
31
- unless valid_scheme && valid_no_local && valid_suffix
32
- record.errors.add(attribute, options.fetch(:message), value: value)
33
- end
34
- rescue URI::InvalidURIError
35
- 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
36
39
  end
40
+
41
+ validate_url(record, attribute, value, schemes)
37
42
  end
38
43
 
39
44
  protected
@@ -43,6 +48,23 @@ module ActiveModel
43
48
  filtered[:value] = value
44
49
  filtered
45
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
46
68
  end
47
69
 
48
70
  module ClassMethods
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanel Suurhans
8
8
  - Tarmo Lehtpuu
9
9
  - Vladimir Krylov
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-01 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
@@ -55,47 +55,61 @@ dependencies:
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  - !ruby/object:Gem::Dependency
58
- name: activemodel
58
+ name: rspec
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - ">="
62
62
  - !ruby/object:Gem::Version
63
- version: 3.0.0
64
- type: :runtime
63
+ version: '0'
64
+ type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
- version: 3.0.0
70
+ version: '0'
71
71
  - !ruby/object:Gem::Dependency
72
- name: rspec
72
+ name: diff-lcs
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 1.1.2
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="
83
83
  - !ruby/object:Gem::Version
84
- version: '0'
84
+ version: 1.1.2
85
85
  - !ruby/object:Gem::Dependency
86
- name: diff-lcs
86
+ name: activemodel
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - ">="
90
90
  - !ruby/object:Gem::Version
91
- version: 1.1.2
92
- type: :development
91
+ version: 3.0.0
92
+ type: :runtime
93
93
  prerelease: false
94
94
  version_requirements: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
- version: 1.1.2
98
+ version: 3.0.0
99
+ - !ruby/object:Gem::Dependency
100
+ name: public_suffix
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
99
113
  description: Library for validating urls in Rails.
100
114
  email:
101
115
  - tanel.suurhans@perfectline.co
@@ -111,24 +125,31 @@ files:
111
125
  - README.md
112
126
  - init.rb
113
127
  - install.rb
128
+ - lib/locale/ar.yml
114
129
  - lib/locale/de.yml
115
130
  - lib/locale/en.yml
131
+ - lib/locale/es.yml
116
132
  - lib/locale/fr.yml
117
133
  - lib/locale/it.yml
118
134
  - lib/locale/ja.yml
119
135
  - lib/locale/km.yml
136
+ - lib/locale/nl.yml
137
+ - lib/locale/pl.yml
120
138
  - lib/locale/pt-BR.yml
139
+ - lib/locale/pt-PT.yml
140
+ - lib/locale/pt.yml
121
141
  - lib/locale/ro.yml
122
142
  - lib/locale/ru.yml
123
143
  - lib/locale/tr.yml
144
+ - lib/locale/vi.yml
124
145
  - lib/locale/zh-CN.yml
125
146
  - lib/locale/zh-TW.yml
126
- - lib/rspec_matcher.rb
127
147
  - lib/validate_url.rb
148
+ - lib/validate_url/rspec_matcher.rb
128
149
  homepage: http://github.com/perfectline/validates_url/tree/master
129
150
  licenses: []
130
151
  metadata: {}
131
- post_install_message:
152
+ post_install_message:
132
153
  rdoc_options: []
133
154
  require_paths:
134
155
  - lib
@@ -143,9 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
164
  - !ruby/object:Gem::Version
144
165
  version: '0'
145
166
  requirements: []
146
- rubyforge_project:
147
- rubygems_version: 2.4.5.1
148
- signing_key:
167
+ rubygems_version: 3.0.8
168
+ signing_key:
149
169
  specification_version: 4
150
170
  summary: Library for validating urls in Rails.
151
171
  test_files: []