can_has_validations 1.0.0 → 1.0.1

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: 98d343f6a4e3bded76cac6973926550938ba481bc9357585c3d83fb8427f48e1
4
- data.tar.gz: dc5648966508e9db4ff6972475f1df6b813fa9beb7bd5b83192eb5483ad09e08
3
+ metadata.gz: eb1dc3533f35f980fd7061b9ac1aaa3b8cf4f9c43af0ed1d7d6d4fb898bf7931
4
+ data.tar.gz: 02d171105d2abf207de0a182e0f7a81344afafb0c8191d2e186929f4dfe0344a
5
5
  SHA512:
6
- metadata.gz: 4b6bf6510a992f70ffb418010bc05ae0f97ddb181c7d0482c93ff87756abac7024b8c3e9cf32caee7cfefac7a93bf60b0292475732ea9dc3a39a2009196f6893
7
- data.tar.gz: 4a157d72a5ec06a71e3e07a93e4f9fa009fedc7e637b4188d7a7aad2aadb3f55b612e94f3eb3a54dcd1581874ae646f001d00efa956e31e2eb65633b5551a276
6
+ metadata.gz: ef067e3db2f6f0d157390794db619f77048c3cce20951efd2ee17276be321fad01f9923c984b268f9d95db51235a1afb191d6d3d763cae245176adcc39e73f70
7
+ data.tar.gz: b46c618a0b3d4cea672626766b71944ccef2e2150495bbc2de06329edf4c1ff521e0903e18dde409404af7f32a0561887d6df45d3dba907906b8397e500a9b2f
@@ -1,15 +1,54 @@
1
1
  # Ensure an attribute is generally formatted as an email.
2
2
  # eg: validates :user_email, email: true
3
3
 
4
+ require_relative 'hostname_validator'
5
+
4
6
  module ActiveModel::Validations
5
7
  class EmailValidator < ActiveModel::EachValidator
6
8
 
7
- EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
9
+ EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
10
+ SEGMENT_REGEXP = /\A[a-z0-9+_-]+\z/i
11
+ LABEL_REGEXP = HostnameValidator::LABEL_REGEXP
12
+ FINAL_LABEL_REGEXP = HostnameValidator::FINAL_LABEL_REGEXP
8
13
 
9
14
  def validate_each(record, attribute, value)
10
- unless value =~ EMAIL_REGEXP
15
+ unless email_valid?(value)
11
16
  record.errors.add(attribute, :invalid_email, options.merge(value: value))
12
17
  end
13
18
  end
19
+
20
+ def email_valid?(value)
21
+ recipient, domain = value.split('@', 2)
22
+ is_valid = true
23
+
24
+ recipient ||= ''
25
+ is_valid &&= recipient.length <= 255
26
+ is_valid &&= recipient !~ /\.\./
27
+ is_valid &&= !recipient.starts_with?('.')
28
+ is_valid &&= !recipient.ends_with?('.')
29
+ recipient.split('.').each do |segment|
30
+ is_valid &&= segment =~ SEGMENT_REGEXP
31
+ end
32
+
33
+ domain ||= ''
34
+ if defined?(Addressable::IDNA)
35
+ domain &&= Addressable::IDNA.to_ascii(domain)
36
+ end
37
+ labels = domain.split('.')
38
+ is_valid &&= domain.length <= 255
39
+ is_valid &&= domain !~ /\.\./
40
+ is_valid &&= labels.size.in? 2..100
41
+ labels.each_with_index do |label, idx|
42
+ is_valid &&= label.length <= 63
43
+ if idx+1==labels.size
44
+ is_valid &&= label =~ FINAL_LABEL_REGEXP
45
+ else
46
+ is_valid &&= label =~ LABEL_REGEXP
47
+ end
48
+ end
49
+
50
+ is_valid
51
+ end
52
+
14
53
  end
15
54
  end
@@ -1,3 +1,3 @@
1
1
  module CanHasValidations
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
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.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thomas morgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
11
+ date: 2019-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails