validators 2.5.4 → 2.6.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
  SHA1:
3
- metadata.gz: 239b1fb222056d2e4b90c6a67f067bf75b013090
4
- data.tar.gz: 9ebe96f85ffd7515c30fe17ad82d0302cf792e92
3
+ metadata.gz: a8737099eb2305ac923c29c1e2848b629f5a3f3a
4
+ data.tar.gz: 801137ecb014fcbf664e10a5a32899b19ac97a68
5
5
  SHA512:
6
- metadata.gz: 8510fc2a25f03d90a36ac81b0d051d903863235fccdce381347c6e64831ca1c89d243c8b1c310d4b7933916bf93f3770c19002fd05412f1cc895714197f04720
7
- data.tar.gz: f728920d75be8ba9d104db10e5d17a2e9f3919e8015aa70f54519fa67fb45222b729f0bff5093ebe40a09e23710c0a4e1af563ff463eb41163ad4e52018f277f
6
+ metadata.gz: 96b81484e99d3dbc9094e4915510a72e5a00400089ae8c8ea0db75ae3e0d93f5310e6f28663b3c918d27084aa8c6b3d5c33e5f6fe97fdae5ebe286ac08666423
7
+ data.tar.gz: 511a3e0fbfbc6c9aea01c7286bdc0cd99e7a5de5ef98dc03d6e08509e86c49c27a09e094d475e745456755bdf88881d75b7640c40f674148ca1053f3cdccd775
data/README.md CHANGED
@@ -135,6 +135,22 @@ class Server < ActiveRecord::Base
135
135
  end
136
136
  ```
137
137
 
138
+ ### validates_subdomain
139
+
140
+ Rules:
141
+
142
+ - maximum length is 63 characters
143
+ - characters allowed are a-z, A-Z, 0-9 and hyphen
144
+ - labels do not begin or end with a hyphen
145
+ - labels do not consist of numeric values only
146
+
147
+
148
+ ```ruby
149
+ class Site < ActiveRecord::Base
150
+ validates_subdomain :slug
151
+ end
152
+ ```
153
+
138
154
  ## Maintainer
139
155
 
140
156
  * [Nando Vieira](http://simplesideias.com.br)
data/lib/validators.rb CHANGED
@@ -15,4 +15,5 @@ module Validators
15
15
  require "validators/validates_ssh_private_key"
16
16
  require "validators/validates_ssh_public_key"
17
17
  require "validators/validates_hostname_format_of"
18
+ require "validators/validates_subdomain_format_of"
18
19
  end
@@ -0,0 +1,38 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class SubdomainValidator < EachValidator
4
+ # Rules taken from http://www.zytrax.com/books/dns/apa/names.html
5
+ def validate_each(record, attribute, value)
6
+ return if value.blank? && options[:allow_blank]
7
+ return if value.nil? && options[:allow_nil]
8
+ return if valid_label?(value.to_s)
9
+
10
+ record.errors.add(attribute, :invalid_subdomain,
11
+ message: options[:message],
12
+ value: value
13
+ )
14
+ end
15
+
16
+ def valid_label?(label)
17
+ !label.start_with?("-") &&
18
+ !label.match(/\A\d+\z/) &&
19
+ label.match(/\A[a-z0-9-]{1,63}\z/i)
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ # Validates whether or not the specified URL is valid.
25
+ #
26
+ # class User < ActiveRecord::Base
27
+ # validates_subdomain_format_of :subdomain
28
+ # validates_subdomain :subdomain
29
+ # end
30
+ #
31
+ def validates_subdomain_format_of(*attr_names)
32
+ validates_with SubdomainValidator, _merge_attributes(attr_names)
33
+ end
34
+
35
+ alias_method :validates_subdomain, :validates_subdomain_format_of
36
+ end
37
+ end
38
+ end
@@ -1,8 +1,8 @@
1
1
  module Validators
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 5
5
- PATCH = 4
4
+ MINOR = 6
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -57,3 +57,14 @@ class ServerWithTLD
57
57
  @host = host
58
58
  end
59
59
  end
60
+
61
+ class Site
62
+ include ActiveModel::Validations
63
+ attr_accessor :slug
64
+
65
+ validates_subdomain :slug
66
+
67
+ def initialize(slug)
68
+ @slug = slug
69
+ end
70
+ end
@@ -0,0 +1,18 @@
1
+ VALID_SUBDOMAINS = %W[
2
+ #{'a'*63}
3
+ mysite
4
+ my-site
5
+ mysite-
6
+ mysite1234
7
+ 1234mysite
8
+ MYSITE
9
+ MYSITE1234
10
+ 1234MYSITE
11
+ mysite--
12
+ ]
13
+
14
+ INVALID_SUBDOMAINS = %W[
15
+ -mysite
16
+ 1234
17
+ #{'a'*64}
18
+ ]
@@ -4,11 +4,14 @@ class ValidatesDatetimeBeforeOptionTest < Minitest::Test
4
4
  let(:user) { User.new }
5
5
 
6
6
  test "rejects when date is set to after :before option" do
7
- User.validates_datetime :registered_at, :before => 1.week.ago
8
- user.registered_at = Time.now
7
+ week_ago = 1.week.ago
8
+ now = Time.now
9
+
10
+ User.validates_datetime :registered_at, :before => week_ago
11
+ user.registered_at = now
9
12
 
10
13
  refute user.valid?
11
- assert_includes user.errors[:registered_at], "needs to be before #{I18n.l(1.week.ago)}"
14
+ assert_includes user.errors[:registered_at], "needs to be before #{I18n.l(week_ago)}"
12
15
  end
13
16
 
14
17
  test "accepts when date is set accordingly to the :before option" do
@@ -0,0 +1,17 @@
1
+ require "test_helper"
2
+
3
+ class ValidatesSubdomainTest < Minitest::Test
4
+ VALID_SUBDOMAINS.each do |subdomain|
5
+ test "accepts #{subdomain}" do
6
+ site = Site.new(subdomain)
7
+ assert site.valid?
8
+ end
9
+ end
10
+
11
+ INVALID_SUBDOMAINS.each do |subdomain|
12
+ test "rejects #{subdomain}" do
13
+ site = Site.new(subdomain)
14
+ refute site.valid?
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.4
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-29 00:00:00.000000000 Z
11
+ date: 2017-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -154,6 +154,7 @@ files:
154
154
  - lib/validators/validates_ownership_of.rb
155
155
  - lib/validators/validates_ssh_private_key.rb
156
156
  - lib/validators/validates_ssh_public_key.rb
157
+ - lib/validators/validates_subdomain_format_of.rb
157
158
  - lib/validators/validates_url_format_of.rb
158
159
  - lib/validators/version.rb
159
160
  - test/schema.rb
@@ -162,6 +163,7 @@ files:
162
163
  - test/support/hostnames.rb
163
164
  - test/support/ips.rb
164
165
  - test/support/models.rb
166
+ - test/support/subdomains.rb
165
167
  - test/support/translations.yml
166
168
  - test/support/urls.rb
167
169
  - test/test_helper.rb
@@ -184,6 +186,7 @@ files:
184
186
  - test/validators/validates_ssh_private_key/dsa_test.rb
185
187
  - test/validators/validates_ssh_private_key/rsa_test.rb
186
188
  - test/validators/validates_ssh_public_key_test.rb
189
+ - test/validators/validates_subdomain_test.rb
187
190
  - test/validators/validates_url_format_of/with_tld_validation_test.rb
188
191
  - test/validators/validates_url_format_of/without_tld_validation_test.rb
189
192
  - validators.gemspec
@@ -218,6 +221,7 @@ test_files:
218
221
  - test/support/hostnames.rb
219
222
  - test/support/ips.rb
220
223
  - test/support/models.rb
224
+ - test/support/subdomains.rb
221
225
  - test/support/translations.yml
222
226
  - test/support/urls.rb
223
227
  - test/test_helper.rb
@@ -240,5 +244,6 @@ test_files:
240
244
  - test/validators/validates_ssh_private_key/dsa_test.rb
241
245
  - test/validators/validates_ssh_private_key/rsa_test.rb
242
246
  - test/validators/validates_ssh_public_key_test.rb
247
+ - test/validators/validates_subdomain_test.rb
243
248
  - test/validators/validates_url_format_of/with_tld_validation_test.rb
244
249
  - test/validators/validates_url_format_of/without_tld_validation_test.rb