can_has_validations 1.6.0 → 1.8.0
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
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c78f8e5cbb5c2bd034070fad41bbaf78b622430b2c0546b6cf2cb22f05fe0d93
|
4
|
+
data.tar.gz: e852f21850a91d4a0f348b96b913c315de7d663d812ac05b30f16917b216ad5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef126f2bd28db8947e189adef8572ca63d3cd964ab19732fd1291d49315c703b0f386793aeb2df22c4cea97ef195be9b6e6affacb91a49748f0df9cc05152cc3
|
7
|
+
data.tar.gz: 920a6ca45c368c4e3ddd077018b0ea67bb3bc11a46ca0855480077d3e62c8c5ccccbb6b55da1fbf99b3b727d9408f264b3744d6f938b9e6db1bfef390ff25434
|
data/MIT-LICENSE
CHANGED
@@ -1,38 +1,40 @@
|
|
1
1
|
# Ensure an attribute is generally formatted as an email.
|
2
2
|
# eg: validates :user_email, email: true
|
3
|
+
# validates :user_email, email: {allow_unicode: true}
|
3
4
|
|
4
5
|
require_relative 'hostname_validator'
|
5
6
|
|
6
7
|
module ActiveModel::Validations
|
7
8
|
class EmailValidator < ActiveModel::EachValidator
|
8
9
|
|
9
|
-
EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[
|
10
|
-
SEGMENT_REGEXP = /\A[a-z0-
|
11
|
-
LABEL_REGEXP =
|
10
|
+
EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[a-z0-9-]+\.)+[a-z]{2,})\z/i
|
11
|
+
SEGMENT_REGEXP = /\A[a-z0-9_+-]+\z/i
|
12
|
+
LABEL_REGEXP = %r{\A([a-zA-Z0-9]([a-zA-Z0-9-]+)?)?[a-zA-Z0-9]\z}
|
13
|
+
# HostnameValidator::LABEL_REGEXP minus _/
|
12
14
|
FINAL_LABEL_REGEXP = HostnameValidator::FINAL_LABEL_REGEXP
|
13
15
|
|
14
16
|
def validate_each(record, attribute, value)
|
15
|
-
unless email_valid?(value)
|
17
|
+
unless email_valid?(value, **options.slice(:allow_unicode))
|
16
18
|
record.errors.add(attribute, :invalid_email, **options.merge(value: value))
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
|
-
def email_valid?(value)
|
22
|
+
def email_valid?(value, allow_unicode: false)
|
21
23
|
return unless value
|
22
|
-
recipient, domain = value.split('@', 2)
|
24
|
+
recipient, domain = value.to_s.split('@', 2)
|
23
25
|
is_valid = true
|
24
26
|
|
25
27
|
recipient ||= ''
|
26
28
|
is_valid &&= recipient.length <= 255
|
27
29
|
is_valid &&= recipient !~ /\.\./
|
28
|
-
is_valid &&= !recipient.starts_with?('.')
|
29
|
-
is_valid &&= !recipient.ends_with?('.')
|
30
|
+
is_valid &&= !recipient.starts_with?('.')
|
31
|
+
is_valid &&= !recipient.ends_with?('.')
|
30
32
|
recipient.split('.').each do |segment|
|
31
33
|
is_valid &&= segment =~ SEGMENT_REGEXP
|
32
34
|
end
|
33
35
|
|
34
36
|
domain ||= ''
|
35
|
-
if defined?(Addressable::IDNA)
|
37
|
+
if allow_unicode && defined?(Addressable::IDNA)
|
36
38
|
domain &&= Addressable::IDNA.to_ascii(domain)
|
37
39
|
end
|
38
40
|
labels = domain.split('.')
|
@@ -3,26 +3,59 @@
|
|
3
3
|
# eg: validates :website, url: true
|
4
4
|
# validates :redis, url: {scheme: 'redis'}
|
5
5
|
# validates :database, url: {scheme: %w(postgres mysql)}
|
6
|
+
# validates :website, url: {host: 'example.com'}
|
7
|
+
# validates :database, url: {port: [5432, nil]}
|
8
|
+
# to allow scheme's default port, must specify `nil` too
|
9
|
+
# :scheme defaults to `%w(http https)`
|
10
|
+
# :host defaults to `nil` which allows any
|
11
|
+
# :port defaults to `nil` which allows any
|
12
|
+
# to require blank, use `port: false` or `port: [nil]`
|
6
13
|
|
7
14
|
module ActiveModel::Validations
|
8
15
|
class UrlValidator < ActiveModel::EachValidator
|
9
16
|
def validate_each(record, attribute, value)
|
10
|
-
allowed_schemes = if options[:scheme].respond_to?(:call)
|
11
|
-
options[:scheme].call(record)
|
12
|
-
elsif options[:scheme].is_a?(Symbol)
|
13
|
-
record.send(options[:scheme])
|
14
|
-
else
|
15
|
-
Array.wrap(options[:scheme] || %w(http https))
|
16
|
-
end
|
17
|
-
|
18
17
|
if defined?(Addressable::URI)
|
19
18
|
u = Addressable::URI.parse(value) rescue nil
|
20
19
|
u2 = u && URI.parse(u.normalize.to_s) rescue nil
|
21
20
|
else
|
22
21
|
u2 = u = URI.parse(value) rescue nil
|
23
22
|
end
|
24
|
-
|
25
|
-
|
23
|
+
|
24
|
+
allowed_schemes =
|
25
|
+
if options[:scheme].respond_to?(:call)
|
26
|
+
options[:scheme].call(record)
|
27
|
+
elsif options[:scheme].is_a?(Symbol)
|
28
|
+
record.send(options[:scheme])
|
29
|
+
else
|
30
|
+
Array.wrap(options[:scheme] || %w(http https))
|
31
|
+
end
|
32
|
+
|
33
|
+
allowed_hosts =
|
34
|
+
if options[:host].respond_to?(:call)
|
35
|
+
options[:host].call(record)
|
36
|
+
elsif options[:host].is_a?(Symbol)
|
37
|
+
record.send(options[:host])
|
38
|
+
elsif options[:host].nil?
|
39
|
+
[u&.host]
|
40
|
+
else
|
41
|
+
Array.wrap(options[:host])
|
42
|
+
end
|
43
|
+
|
44
|
+
allowed_ports =
|
45
|
+
if options[:port].respond_to?(:call)
|
46
|
+
options[:port].call(record)
|
47
|
+
elsif options[:port].is_a?(Symbol)
|
48
|
+
record.send(options[:port])
|
49
|
+
elsif options[:port].nil?
|
50
|
+
[u&.port]
|
51
|
+
elsif options[:port] == false
|
52
|
+
[nil]
|
53
|
+
else
|
54
|
+
Array.wrap(options[:port])
|
55
|
+
end
|
56
|
+
|
57
|
+
if !u || !u2 || u.relative? || allowed_schemes.exclude?(u.scheme) || allowed_hosts.exclude?(u.host) || allowed_ports.exclude?(u.port)
|
58
|
+
record.errors.add(attribute, :invalid_url, **options.merge(value: value, scheme: allowed_schemes, host: allowed_hosts, port: allowed_ports))
|
26
59
|
end
|
27
60
|
end
|
28
61
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -2,5 +2,22 @@
|
|
2
2
|
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
3
3
|
---------------------------------
|
4
4
|
CanHasValidationsTest: test_truth
|
5
|
+
---------------------------------
|
6
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
7
|
+
[1m[35m (0.8ms)[0m [1m[34mSELECT sqlite_version(*)[0m
|
8
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
9
|
+
---------------------------------
|
10
|
+
CanHasValidationsTest: test_truth
|
11
|
+
---------------------------------
|
12
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
13
|
+
[1m[35m (0.6ms)[0m [1m[34mSELECT sqlite_version(*)[0m
|
14
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
15
|
+
---------------------------------
|
16
|
+
CanHasValidationsTest: test_truth
|
17
|
+
---------------------------------
|
18
|
+
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
19
|
+
[1m[36mTRANSACTION (0.1ms)[0m [1m[36mbegin transaction[0m
|
20
|
+
---------------------------------
|
21
|
+
CanHasValidationsTest: test_truth
|
5
22
|
---------------------------------
|
6
23
|
[1m[36mTRANSACTION (0.0ms)[0m [1m[31mrollback transaction[0m
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
- !ruby/object:Gem::Version
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
|
-
rubygems_version: 3.
|
150
|
+
rubygems_version: 3.3.26
|
151
151
|
signing_key:
|
152
152
|
specification_version: 4
|
153
153
|
summary: Assorted Rails 5.x-7.x validators
|