missing_validators 0.6.0 → 0.6.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.
data/README.md
CHANGED
@@ -72,7 +72,13 @@ You can specify domains to which the URL domain should belong in one of the folo
|
|
72
72
|
validates :url, url: { domains: 'com' }
|
73
73
|
validates :url, url: { domains: :com }
|
74
74
|
validates :url, url: { domains: [:com, 'edu'] }
|
75
|
+
|
76
|
+
You can specify if the URL should the site root:
|
77
|
+
|
75
78
|
validates :url, url: { root: true }
|
79
|
+
|
80
|
+
You can specify the URL scheme:
|
81
|
+
|
76
82
|
validates :url, url: { scheme: :http }
|
77
83
|
validates :url, url: { scheme: [:http, 'https'] }
|
78
84
|
|
@@ -15,14 +15,24 @@ class EmailValidator < ActiveModel::EachValidator
|
|
15
15
|
allow_blank = options[:allow_blank] || false
|
16
16
|
return if allow_blank && value.blank?
|
17
17
|
|
18
|
-
|
19
|
-
email = value && value.downcase || ''
|
20
|
-
in_valid_domain = domains.empty? ? true : domains.any? { |domain| email.end_with?(".#{domain.downcase}") }
|
21
|
-
|
22
|
-
has_valid_format = !!(value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
|
23
|
-
|
24
|
-
unless in_valid_domain && has_valid_format
|
18
|
+
unless valid?(value, options)
|
25
19
|
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email'))
|
26
20
|
end
|
27
21
|
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def valid?(email, options)
|
26
|
+
validate_format(email) \
|
27
|
+
&& validate_domain(email, Array.wrap(options[:domain]))
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_format(email)
|
31
|
+
!!(email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate_domain(email, domains)
|
35
|
+
email_downcased = email.to_s.downcase
|
36
|
+
domains.empty? || domains.any? { |domain| email_downcased.end_with?(".#{domain.downcase}") }
|
37
|
+
end
|
28
38
|
end
|
@@ -14,32 +14,34 @@ class UrlValidator < ActiveModel::EachValidator
|
|
14
14
|
# @param [Object] value attribute value
|
15
15
|
def validate_each(record, attribute, value)
|
16
16
|
uri = URI.parse(value)
|
17
|
-
|
18
|
-
unless uri.kind_of?(URI::HTTP) \
|
19
|
-
&& in_valid_top_level_domains?(uri, Array.wrap(options[:domain])) \
|
20
|
-
&& with_valid_scheme?(uri, Array.wrap(options[:scheme])) \
|
21
|
-
&& (!!!options[:root] || is_root?(uri))
|
22
|
-
|
23
|
-
raise URI::InvalidURIError
|
24
|
-
end
|
25
|
-
|
17
|
+
raise URI::InvalidURIError unless valid?(uri, options)
|
26
18
|
rescue URI::InvalidURIError
|
27
19
|
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.url'))
|
28
20
|
end
|
29
21
|
|
30
22
|
private
|
31
23
|
|
32
|
-
|
33
|
-
|
34
|
-
|
24
|
+
DEFAULT_SCHEMES = [:http, :https]
|
25
|
+
|
26
|
+
def valid?(uri, options)
|
27
|
+
uri.kind_of?(URI::Generic) \
|
28
|
+
&& validate_domain(uri, Array.wrap(options[:domain])) \
|
29
|
+
&& validate_scheme(uri, Array.wrap(options[:scheme] || UrlValidator::DEFAULT_SCHEMES)) \
|
30
|
+
&& validate_root(uri, !!options[:root])
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_domain(uri, domains)
|
34
|
+
host_downcased = uri.host.to_s.downcase
|
35
|
+
domains.empty? || domains.any? { |domain| host_downcased.end_with?(".#{domain.downcase}") }
|
35
36
|
end
|
36
37
|
|
37
|
-
def
|
38
|
-
scheme_downcased = uri.scheme.downcase
|
39
|
-
schemes.empty? || schemes.
|
38
|
+
def validate_scheme(uri, schemes)
|
39
|
+
scheme_downcased = uri.scheme.to_s.downcase
|
40
|
+
schemes.empty? || schemes.any? { |scheme| scheme_downcased == scheme.to_s.downcase }
|
40
41
|
end
|
41
42
|
|
42
|
-
def
|
43
|
+
def validate_root(uri, should_validate)
|
44
|
+
return true unless should_validate
|
43
45
|
['/', ''].include?(uri.path) && uri.query.blank? && uri.fragment.blank?
|
44
46
|
end
|
45
47
|
end
|
@@ -33,6 +33,7 @@ describe UrlValidator do
|
|
33
33
|
it { should_not allow_value("http://user_examplecom").for(:url) }
|
34
34
|
it { should_not allow_value("http://user_example.com").for(:url) }
|
35
35
|
it { should_not allow_value("http://user_example.a").for(:url) }
|
36
|
+
it { should_not allow_value("ftp://foo.bar.baz.com").for(:url) }
|
36
37
|
end
|
37
38
|
|
38
39
|
describe "url must be in a specific domain" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missing_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70244538802020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70244538802020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: shoulda-matchers
|
27
|
-
requirement: &
|
27
|
+
requirement: &70244538801180 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70244538801180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &70244538800500 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 3.0.0
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70244538800500
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activesupport
|
49
|
-
requirement: &
|
49
|
+
requirement: &70244538799780 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 3.0.0
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70244538799780
|
58
58
|
description: Validates email addresses, URLs, and inequality of attributes.
|
59
59
|
email:
|
60
60
|
- andrew.gridnev@gmail.com
|