validate_url 1.0.2 → 1.0.3
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 +18 -12
- data/lib/locale/fr.yml +4 -0
- data/lib/locale/km.yml +4 -0
- data/lib/locale/ro.yml +4 -0
- data/lib/locale/ru.yml +4 -0
- data/lib/locale/zh-CN.yml +4 -0
- data/lib/locale/zh-TW.yml +4 -0
- data/lib/rspec_matcher.rb +12 -0
- data/lib/validate_url.rb +29 -14
- metadata +75 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5f0f637da31e1ad9ccb51ac9dee234805c35ccbc
|
|
4
|
+
data.tar.gz: d2222f7e3844215454a43626c1d283a62a86d13a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8e39fcb677c0f7f96083bc4aaa37d5ca17fc9d0b375ac7f0d1ffb57d81ab3c827d5c4d4f4e979f4924c896a9f51a58860aa8c9eb82ac96843f5a0a4e23d4a31d
|
|
7
|
+
data.tar.gz: 300b61a831028476ccf33182a82c50ebba2cca03d1bc75e9710d7b316fae1ca0d239a3c8e0a9d86396e254fc48ebc4bf71b1a86bd56106af6e6eb8c9608b9a22
|
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# Validates URL
|
|
2
2
|
|
|
3
|
-
This gem adds the capability of validating URLs to ActiveRecord and ActiveModel
|
|
3
|
+
This gem adds the capability of validating URLs to ActiveRecord and ActiveModel.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
|
-
|
|
7
|
-
```
|
|
6
|
+
|
|
7
|
+
```
|
|
8
8
|
# add this to your Gemfile
|
|
9
9
|
gem "validate_url"
|
|
10
10
|
|
|
@@ -16,19 +16,25 @@ sudo gem install validate_url
|
|
|
16
16
|
|
|
17
17
|
### With ActiveRecord
|
|
18
18
|
|
|
19
|
-
```ruby
|
|
19
|
+
```ruby
|
|
20
20
|
class Pony < ActiveRecord::Base
|
|
21
21
|
# standard validation
|
|
22
|
-
validates :homepage, :
|
|
22
|
+
validates :homepage, url: true
|
|
23
23
|
|
|
24
24
|
# with allow_nil
|
|
25
|
-
validates :homepage, :
|
|
25
|
+
validates :homepage, url: { allow_nil: true }
|
|
26
26
|
|
|
27
27
|
# with allow_blank
|
|
28
|
-
validates :homepage, :
|
|
28
|
+
validates :homepage, url: { allow_blank: true }
|
|
29
29
|
|
|
30
30
|
# without local hostnames
|
|
31
|
-
validates :homepage, :
|
|
31
|
+
validates :homepage, url: { no_local: true }
|
|
32
|
+
|
|
33
|
+
# with custom schemes
|
|
34
|
+
validates :homepage, url: { schemes: ['https'] }
|
|
35
|
+
|
|
36
|
+
# with public suffix database https://publicsuffix.org/
|
|
37
|
+
validates :homepage, url: { public_suffix: true }
|
|
32
38
|
end
|
|
33
39
|
```
|
|
34
40
|
|
|
@@ -41,14 +47,14 @@ class Unicorn
|
|
|
41
47
|
attr_accessor :homepage
|
|
42
48
|
|
|
43
49
|
# with legacy syntax (the syntax above works also)
|
|
44
|
-
validates_url :homepage, :
|
|
50
|
+
validates_url :homepage, allow_blank: true
|
|
45
51
|
end
|
|
46
52
|
```
|
|
47
53
|
|
|
48
54
|
### I18n
|
|
49
55
|
|
|
50
|
-
The default error message `is not valid
|
|
51
|
-
You can pass the
|
|
56
|
+
The default error message `is not a valid URL`.
|
|
57
|
+
You can pass the `message: "my custom error"` option to your validation to define your own, custom message.
|
|
52
58
|
|
|
53
59
|
|
|
54
60
|
## Contributing
|
|
@@ -63,4 +69,4 @@ Validates URL is created and maintained by [PerfectLine](http://www.perfectline.
|
|
|
63
69
|
## License
|
|
64
70
|
|
|
65
71
|
Validates URL is Copyright © 2010-2014 [PerfectLine](http://www.perfectline.co), LLC. It is free software, and may be
|
|
66
|
-
redistributed under the terms specified in the LICENSE file.
|
|
72
|
+
redistributed under the terms specified in the LICENSE file.
|
data/lib/locale/fr.yml
ADDED
data/lib/locale/km.yml
ADDED
data/lib/locale/ro.yml
ADDED
data/lib/locale/ru.yml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
RSpec::Matchers.define :validate_url_of do |attribute|
|
|
2
|
+
match do
|
|
3
|
+
actual = subject.is_a?(Class) ? subject.new : subject
|
|
4
|
+
actual.send(:"#{attribute}=", "htp://invalidurl")
|
|
5
|
+
expect(actual).to be_invalid
|
|
6
|
+
@expected_message ||= I18n.t("errors.messages.url")
|
|
7
|
+
expect(actual.errors.messages[attribute.to_sym]).to include(@expected_message)
|
|
8
|
+
end
|
|
9
|
+
chain :with_message do |message|
|
|
10
|
+
@expected_message = message
|
|
11
|
+
end
|
|
12
|
+
end
|
data/lib/validate_url.rb
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
require 'addressable/uri'
|
|
2
1
|
require 'active_model'
|
|
3
2
|
require 'active_support/i18n'
|
|
4
|
-
|
|
5
|
-
I18n.load_path
|
|
6
|
-
I18n.load_path << File.dirname(__FILE__) + '/locale/ja.yml'
|
|
3
|
+
require 'public_suffix'
|
|
4
|
+
I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
|
|
7
5
|
|
|
8
6
|
module ActiveModel
|
|
9
7
|
module Validations
|
|
10
8
|
class UrlValidator < ActiveModel::EachValidator
|
|
9
|
+
RESERVED_OPTIONS = [:schemes, :no_local]
|
|
11
10
|
|
|
12
11
|
def initialize(options)
|
|
13
|
-
options.reverse_merge!(:
|
|
14
|
-
options.reverse_merge!(:
|
|
15
|
-
options.reverse_merge!(:
|
|
12
|
+
options.reverse_merge!(schemes: %w(http https))
|
|
13
|
+
options.reverse_merge!(message: :url)
|
|
14
|
+
options.reverse_merge!(no_local: false)
|
|
15
|
+
options.reverse_merge!(public_suffix: false)
|
|
16
16
|
|
|
17
17
|
super(options)
|
|
18
18
|
end
|
|
@@ -20,14 +20,29 @@ module ActiveModel
|
|
|
20
20
|
def validate_each(record, attribute, value)
|
|
21
21
|
schemes = [*options.fetch(:schemes)].map(&:to_s)
|
|
22
22
|
begin
|
|
23
|
-
uri =
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
uri = URI.parse(value)
|
|
24
|
+
host = uri && uri.host
|
|
25
|
+
scheme = uri && uri.scheme
|
|
26
|
+
|
|
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)
|
|
30
|
+
|
|
31
|
+
unless valid_scheme && valid_no_local && valid_suffix
|
|
32
|
+
record.errors.add(attribute, options.fetch(:message), value: value)
|
|
26
33
|
end
|
|
27
|
-
rescue
|
|
28
|
-
record.errors.add(attribute,
|
|
34
|
+
rescue URI::InvalidURIError
|
|
35
|
+
record.errors.add(attribute, :url, filtered_options(value))
|
|
29
36
|
end
|
|
30
37
|
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
|
|
41
|
+
def filtered_options(value)
|
|
42
|
+
filtered = options.except(*RESERVED_OPTIONS)
|
|
43
|
+
filtered[:value] = value
|
|
44
|
+
filtered
|
|
45
|
+
end
|
|
31
46
|
end
|
|
32
47
|
|
|
33
48
|
module ClassMethods
|
|
@@ -36,8 +51,8 @@ module ActiveModel
|
|
|
36
51
|
# class Unicorn
|
|
37
52
|
# include ActiveModel::Validations
|
|
38
53
|
# attr_accessor :homepage, :ftpsite
|
|
39
|
-
# validates_url :homepage, :
|
|
40
|
-
# validates_url :ftpsite, :
|
|
54
|
+
# validates_url :homepage, allow_blank: true
|
|
55
|
+
# validates_url :ftpsite, schemes: ['ftp']
|
|
41
56
|
# end
|
|
42
57
|
# Configuration options:
|
|
43
58
|
# * <tt>:message</tt> - A custom error message (default is: "is not a valid URL").
|
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.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tanel Suurhans
|
|
@@ -10,30 +10,44 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2019-03-01 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
|
-
name:
|
|
16
|
+
name: validate_url
|
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
|
18
18
|
requirements:
|
|
19
19
|
- - ">="
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version:
|
|
21
|
+
version: '0'
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version:
|
|
28
|
+
version: '0'
|
|
29
|
+
- !ruby/object:Gem::Dependency
|
|
30
|
+
name: diff-lcs
|
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 1.1.2
|
|
36
|
+
type: :development
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 1.1.2
|
|
29
43
|
- !ruby/object:Gem::Dependency
|
|
30
|
-
name:
|
|
44
|
+
name: rake
|
|
31
45
|
requirement: !ruby/object:Gem::Requirement
|
|
32
46
|
requirements:
|
|
33
47
|
- - ">="
|
|
34
48
|
- !ruby/object:Gem::Version
|
|
35
49
|
version: '0'
|
|
36
|
-
type: :
|
|
50
|
+
type: :development
|
|
37
51
|
prerelease: false
|
|
38
52
|
version_requirements: !ruby/object:Gem::Requirement
|
|
39
53
|
requirements:
|
|
@@ -41,7 +55,7 @@ dependencies:
|
|
|
41
55
|
- !ruby/object:Gem::Version
|
|
42
56
|
version: '0'
|
|
43
57
|
- !ruby/object:Gem::Dependency
|
|
44
|
-
name:
|
|
58
|
+
name: jeweler
|
|
45
59
|
requirement: !ruby/object:Gem::Requirement
|
|
46
60
|
requirements:
|
|
47
61
|
- - ">="
|
|
@@ -55,19 +69,61 @@ dependencies:
|
|
|
55
69
|
- !ruby/object:Gem::Version
|
|
56
70
|
version: '0'
|
|
57
71
|
- !ruby/object:Gem::Dependency
|
|
58
|
-
name:
|
|
72
|
+
name: sqlite3
|
|
59
73
|
requirement: !ruby/object:Gem::Requirement
|
|
60
74
|
requirements:
|
|
61
75
|
- - ">="
|
|
62
76
|
- !ruby/object:Gem::Version
|
|
63
|
-
version:
|
|
77
|
+
version: '0'
|
|
64
78
|
type: :development
|
|
65
79
|
prerelease: false
|
|
66
80
|
version_requirements: !ruby/object:Gem::Requirement
|
|
67
81
|
requirements:
|
|
68
82
|
- - ">="
|
|
69
83
|
- !ruby/object:Gem::Version
|
|
70
|
-
version:
|
|
84
|
+
version: '0'
|
|
85
|
+
- !ruby/object:Gem::Dependency
|
|
86
|
+
name: activerecord
|
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '0'
|
|
92
|
+
type: :development
|
|
93
|
+
prerelease: false
|
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
95
|
+
requirements:
|
|
96
|
+
- - ">="
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: '0'
|
|
99
|
+
- !ruby/object:Gem::Dependency
|
|
100
|
+
name: activemodel
|
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
|
102
|
+
requirements:
|
|
103
|
+
- - ">="
|
|
104
|
+
- !ruby/object:Gem::Version
|
|
105
|
+
version: 3.0.0
|
|
106
|
+
type: :runtime
|
|
107
|
+
prerelease: false
|
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: 3.0.0
|
|
113
|
+
- !ruby/object:Gem::Dependency
|
|
114
|
+
name: rspec
|
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
|
116
|
+
requirements:
|
|
117
|
+
- - ">="
|
|
118
|
+
- !ruby/object:Gem::Version
|
|
119
|
+
version: '0'
|
|
120
|
+
type: :development
|
|
121
|
+
prerelease: false
|
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '0'
|
|
71
127
|
description: Library for validating urls in Rails.
|
|
72
128
|
email:
|
|
73
129
|
- tanel.suurhans@perfectline.co
|
|
@@ -85,10 +141,17 @@ files:
|
|
|
85
141
|
- install.rb
|
|
86
142
|
- lib/locale/de.yml
|
|
87
143
|
- lib/locale/en.yml
|
|
144
|
+
- lib/locale/fr.yml
|
|
88
145
|
- lib/locale/it.yml
|
|
89
146
|
- lib/locale/ja.yml
|
|
147
|
+
- lib/locale/km.yml
|
|
90
148
|
- lib/locale/pt-BR.yml
|
|
149
|
+
- lib/locale/ro.yml
|
|
150
|
+
- lib/locale/ru.yml
|
|
91
151
|
- lib/locale/tr.yml
|
|
152
|
+
- lib/locale/zh-CN.yml
|
|
153
|
+
- lib/locale/zh-TW.yml
|
|
154
|
+
- lib/rspec_matcher.rb
|
|
92
155
|
- lib/validate_url.rb
|
|
93
156
|
homepage: http://github.com/perfectline/validates_url/tree/master
|
|
94
157
|
licenses: []
|
|
@@ -109,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
109
172
|
version: '0'
|
|
110
173
|
requirements: []
|
|
111
174
|
rubyforge_project:
|
|
112
|
-
rubygems_version: 2.
|
|
175
|
+
rubygems_version: 2.6.14
|
|
113
176
|
signing_key:
|
|
114
177
|
specification_version: 4
|
|
115
178
|
summary: Library for validating urls in Rails.
|