validators 2.5.4 → 2.6.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 +4 -4
- data/README.md +16 -0
- data/lib/validators.rb +1 -0
- data/lib/validators/validates_subdomain_format_of.rb +38 -0
- data/lib/validators/version.rb +2 -2
- data/test/support/models.rb +11 -0
- data/test/support/subdomains.rb +18 -0
- data/test/validators/validates_datetime/before_option_test.rb +6 -3
- data/test/validators/validates_subdomain_test.rb +17 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8737099eb2305ac923c29c1e2848b629f5a3f3a
|
4
|
+
data.tar.gz: 801137ecb014fcbf664e10a5a32899b19ac97a68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
@@ -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
|
data/lib/validators/version.rb
CHANGED
data/test/support/models.rb
CHANGED
@@ -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
|
-
|
8
|
-
|
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(
|
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.
|
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-
|
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
|