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: 9758f98d0ba52576c225f0ffe184d18fe1b5e00038117faf309a122493f495c3
4
- data.tar.gz: 5efdd9ed78a93c3e6372bd869149b49fb4d38e3421921805981f94f1a0b42444
3
+ metadata.gz: c78f8e5cbb5c2bd034070fad41bbaf78b622430b2c0546b6cf2cb22f05fe0d93
4
+ data.tar.gz: e852f21850a91d4a0f348b96b913c315de7d663d812ac05b30f16917b216ad5a
5
5
  SHA512:
6
- metadata.gz: a054838d0d0e7fd0aa25a805faa82a7708a21e909fa3cbd8e4fef962cca68ea2a4a4e70c81b064e2d23b50d60b5d51795c22e6255596d42a95e09037ac1699f6
7
- data.tar.gz: 805563f53b9f845d06ba6617ed565f28737b204e6d5bdc2efc88b4abf5d38a704bfe33b3ccae75909010846b8b7369fc82a133fd3780c2e07c6c591e5a0a89fc
6
+ metadata.gz: ef126f2bd28db8947e189adef8572ca63d3cd964ab19732fd1291d49315c703b0f386793aeb2df22c4cea97ef195be9b6e6affacb91a49748f0df9cc05152cc3
7
+ data.tar.gz: 920a6ca45c368c4e3ddd077018b0ea67bb3bc11a46ca0855480077d3e62c8c5ccccbb6b55da1fbf99b3b727d9408f264b3744d6f938b9e6db1bfef390ff25434
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2012-2022 thomas morgan
1
+ Copyright 2012-2023 thomas morgan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -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._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
10
- SEGMENT_REGEXP = /\A[a-z0-9+_-]+\z/i
11
- LABEL_REGEXP = HostnameValidator::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
- if !u || !u2 || u.relative? || allowed_schemes.exclude?(u.scheme)
25
- record.errors.add(attribute, :invalid_url, **options.merge(value: value, scheme: allowed_schemes))
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
@@ -1,3 +1,3 @@
1
1
  module CanHasValidations
2
- VERSION = '1.6.0'
2
+ VERSION = '1.8.0'
3
3
  end
@@ -2,5 +2,22 @@
2
2
  TRANSACTION (0.1ms) begin transaction
3
3
  ---------------------------------
4
4
  CanHasValidationsTest: test_truth
5
+ ---------------------------------
6
+ TRANSACTION (0.0ms) rollback transaction
7
+  (0.8ms) SELECT sqlite_version(*)
8
+ TRANSACTION (0.1ms) begin transaction
9
+ ---------------------------------
10
+ CanHasValidationsTest: test_truth
11
+ ---------------------------------
12
+ TRANSACTION (0.0ms) rollback transaction
13
+  (0.6ms) SELECT sqlite_version(*)
14
+ TRANSACTION (0.1ms) begin transaction
15
+ ---------------------------------
16
+ CanHasValidationsTest: test_truth
17
+ ---------------------------------
18
+ TRANSACTION (0.0ms) rollback transaction
19
+ TRANSACTION (0.1ms) begin transaction
20
+ ---------------------------------
21
+ CanHasValidationsTest: test_truth
5
22
  ---------------------------------
6
23
  TRANSACTION (0.0ms) rollback transaction
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.6.0
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: 2022-01-27 00:00:00.000000000 Z
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.2.22
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