can_has_validations 1.6.0 → 1.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9758f98d0ba52576c225f0ffe184d18fe1b5e00038117faf309a122493f495c3
4
- data.tar.gz: 5efdd9ed78a93c3e6372bd869149b49fb4d38e3421921805981f94f1a0b42444
3
+ metadata.gz: bb51b71bb23aabfeb3e1bde9ad0f03119d4011d60c7117971c606266eb0c477a
4
+ data.tar.gz: 5b101d95e237b874ff096f9658c064709aef19228c3444f6ed6878fb1f5cc1c5
5
5
  SHA512:
6
- metadata.gz: a054838d0d0e7fd0aa25a805faa82a7708a21e909fa3cbd8e4fef962cca68ea2a4a4e70c81b064e2d23b50d60b5d51795c22e6255596d42a95e09037ac1699f6
7
- data.tar.gz: 805563f53b9f845d06ba6617ed565f28737b204e6d5bdc2efc88b4abf5d38a704bfe33b3ccae75909010846b8b7369fc82a133fd3780c2e07c6c591e5a0a89fc
6
+ metadata.gz: 82e3dc6177df9d32216bbefbbfeef97ccb4bebf22d5b8431c9d59393fd619f2d465b52f3c26eb84c551f35c154279e54c5689d595e699de0b65d211bc115c0ef
7
+ data.tar.gz: fe709a9a7225cf2ea6eb7828c4d741b11c07181a32710d394972ddb424bc282d44d9f163388451671577ff0ebf96c28b3921f46d60b63e61f8be2384a0702e08
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
@@ -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.7.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.7.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-01-20 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