unders-validates_as_hostname_label 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +17 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +51 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_hostname_label.rb +40 -0
- data/test/validates_as_hostname_label_test.rb +123 -0
- metadata +58 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
2009-06-02 - Anders Törnqvist (anders@elabs.se)
|
2
|
+
* Added i18n
|
3
|
+
* Added a default reserved list
|
4
|
+
|
5
|
+
2009-01-17 - Sean Huber (shuber@huberry.com)
|
6
|
+
* Bump version number so github will rebuild the gem
|
7
|
+
|
8
|
+
2009-01-14 - Sean Huber (shuber@huberry.com)
|
9
|
+
* Allow all other validation options like :allow_nil, :if, :unless, etc
|
10
|
+
* Add support for the :reserved option to exclude certain values
|
11
|
+
|
12
|
+
2009-01-13 - Sean Huber (shuber@huberry.com)
|
13
|
+
* Initial commit
|
14
|
+
* Add logic
|
15
|
+
* Add tests
|
16
|
+
* Add gemspec
|
17
|
+
* Update README
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Sean Huber (shuber@huberry.com)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# validates\_as\_hostname\_label #
|
2
|
+
|
3
|
+
Checks for valid hostname labels by looking for:
|
4
|
+
|
5
|
+
* Length between 1 and 63 characters long
|
6
|
+
* Letters 'a' through 'z' (case-insensitive), the digits '0' through '9', and the hyphen (and optionally the underscore)
|
7
|
+
* Labels that don't begin or end with a hyphen or underscore
|
8
|
+
* Reserved labels
|
9
|
+
|
10
|
+
See [http://en.wikipedia.org/wiki/Hostname#Restrictions\_on\_valid\_host\_names](http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names)
|
11
|
+
|
12
|
+
|
13
|
+
## Installation ##
|
14
|
+
|
15
|
+
gem install shuber-validates_as_hostname_label --source http://gems.github.com
|
16
|
+
OR
|
17
|
+
script/plugin install git://github.com/shuber/validates_as_hostname_label.git
|
18
|
+
|
19
|
+
|
20
|
+
## Usage ##
|
21
|
+
|
22
|
+
class Account < ActiveRecord::Base
|
23
|
+
validates_as_hostname_label :subdomain
|
24
|
+
end
|
25
|
+
|
26
|
+
You may optionally pass a `:reserved` option which should be an array of hostname labels to exclude,
|
27
|
+
otherwise Huberry::ValidatesAsHostnameLabel::RESERVED_HOSTNAMES will be used.
|
28
|
+
|
29
|
+
class Account < ActiveRecord::Base
|
30
|
+
validates_as_hostname_label :subdomain, :reserved => ['www', 'ftp', 'mail', 'pop']
|
31
|
+
end
|
32
|
+
|
33
|
+
@account = Account.new :subdomain => 'www'
|
34
|
+
@account.save # false
|
35
|
+
@account.errors # { :subdomain => 'is reserved' }
|
36
|
+
|
37
|
+
Also accepts an `:allow_underscores` option which defaults to `false`
|
38
|
+
|
39
|
+
class Account < ActiveRecord::Base
|
40
|
+
validates_as_hostname_label :subdomain, :allow_underscores => true
|
41
|
+
end
|
42
|
+
|
43
|
+
@account = Account.new :subdomain => 'test_ing'
|
44
|
+
@account.save # true
|
45
|
+
|
46
|
+
The usual validation options (like `:if`, `:unless`, `:allow_nil`, etc) work as well
|
47
|
+
|
48
|
+
|
49
|
+
## Contact ##
|
50
|
+
|
51
|
+
Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run the validates_as_hostname_label tests'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the validates_as_hostname_label gem/plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the validates_as_hostname_label gem/plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'validates_as_hostname_label'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.markdown')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_hostname_label'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Huberry
|
2
|
+
module ValidatesAsHostnameLabel
|
3
|
+
|
4
|
+
RESERVED_HOSTNAMES = %w(www blog dev stage stats status admin ftp sftp mail pop pop3 imap smtp)
|
5
|
+
|
6
|
+
# Checks for:
|
7
|
+
#
|
8
|
+
# * Length between 1 and 63 characters long
|
9
|
+
# * Letters 'a' through 'z' (case-insensitive), the digits '0' through '9', and the hyphen (and optionally the underscore)
|
10
|
+
# * Labels that don't begin or end with a hyphen or underscore
|
11
|
+
# * Reserved labels
|
12
|
+
#
|
13
|
+
# Accepts an :allow_underscores option which defaults to false
|
14
|
+
def validates_as_hostname_label(*attrs)
|
15
|
+
options = { :allow_underscores => false, :reserved => RESERVED_HOSTNAMES }.merge(attrs.last.is_a?(Hash) ? attrs.pop : {})
|
16
|
+
|
17
|
+
format = 'a-z0-9\-'
|
18
|
+
format << '_' if options.delete(:allow_underscores)
|
19
|
+
|
20
|
+
validates_length_of *attrs + [{
|
21
|
+
:in => 1..63,
|
22
|
+
:message => I18n.t('validates_as_hostname_label.invalid_length', :default => 'must be between 1 and 63 characters long')
|
23
|
+
}.merge(options)]
|
24
|
+
|
25
|
+
validates_exclusion_of *attrs + [{
|
26
|
+
:in => options.delete(:reserved),
|
27
|
+
:message => I18n.t('validates_as_hostname_label.reserved', :default => 'is reserved')
|
28
|
+
}.merge(options)]
|
29
|
+
|
30
|
+
validates_format_of *attrs + [{ :with => /^[#{format}]*$/i }.merge(options)]
|
31
|
+
|
32
|
+
validates_format_of *attrs + [{
|
33
|
+
:with => /^[^-_].*[^-_]$/i,
|
34
|
+
:message => I18n.t('validates_as_hostname_label.invalid_first_character', :default => "can't start or end with a hyphen or underscore")
|
35
|
+
}.merge(options)]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
ActiveRecord::Base.extend Huberry::ValidatesAsHostnameLabel
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'activerecord'
|
3
|
+
require 'active_record'
|
4
|
+
require 'test/unit'
|
5
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_hostname_label'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
8
|
+
|
9
|
+
class Account < ActiveRecord::Base
|
10
|
+
validates_as_hostname_label :subdomain
|
11
|
+
validates_as_hostname_label :subdomain_with_underscores, :allow_underscores => true
|
12
|
+
validates_as_hostname_label :subdomain_with_blank, :allow_blank => true
|
13
|
+
validates_as_hostname_label :subdomain_with_nil, :allow_nil => true
|
14
|
+
validates_as_hostname_label :subdomain_with_reserved, :reserved => ['funky']
|
15
|
+
end
|
16
|
+
|
17
|
+
class ValidatesAsHostnameLabelTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
|
21
|
+
silence_stream(STDOUT) do
|
22
|
+
ActiveRecord::Schema.define(:version => 1) do
|
23
|
+
create_table :accounts do |t|
|
24
|
+
t.string :subdomain
|
25
|
+
t.string :subdomain_with_underscores
|
26
|
+
t.string :subdomain_with_blank
|
27
|
+
t.string :subdomain_with_nil
|
28
|
+
t.string :subdomain_with_reserved
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_save_with_valid_subdomains
|
35
|
+
@account = Account.new :subdomain => 'testing', :subdomain_with_underscores => 'testing', :subdomain_with_nil => 'testing', :subdomain_with_blank => 'testing', :subdomain_with_reserved => 'testing'
|
36
|
+
assert @account.save
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_save_subdomains_with_hyphens
|
40
|
+
@account = Account.new :subdomain => 'test-ing', :subdomain_with_underscores => 'test-ing', :subdomain_with_nil => 'test-ing', :subdomain_with_blank => 'test-ing', :subdomain_with_reserved => 'test-ing'
|
41
|
+
assert @account.save
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_should_not_save_with_blank_subdomain_if_allow_option_is_not_specified
|
45
|
+
@account = Account.new :subdomain => nil
|
46
|
+
assert !@account.save
|
47
|
+
assert @account.errors.on(:subdomain)
|
48
|
+
|
49
|
+
@account.subdomain = ''
|
50
|
+
assert !@account.save
|
51
|
+
assert @account.errors.on(:subdomain)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_save_with_blank_subdomain
|
55
|
+
@account = Account.new :subdomain => 'testing', :subdomain_with_underscores => 'testing', :subdomain_with_nil => 'testing', :subdomain_with_blank => '', :subdomain_with_reserved => 'testing'
|
56
|
+
assert @account.save
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_should_save_with_nil_subdomain
|
60
|
+
@account = Account.new :subdomain => 'testing', :subdomain_with_underscores => 'testing', :subdomain_with_nil => nil, :subdomain_with_blank => 'testing', :subdomain_with_reserved => 'testing'
|
61
|
+
assert @account.save
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_not_save_with_too_long_of_a_subdomain
|
65
|
+
@account = Account.new :subdomain => ('t' * 64)
|
66
|
+
assert !@account.save
|
67
|
+
assert @account.errors.on(:subdomain)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_not_save_with_invalid_characters
|
71
|
+
@account = Account.new :subdomain => '!@#$%^&*'
|
72
|
+
assert !@account.save
|
73
|
+
assert @account.errors.on(:subdomain)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_should_not_save_with_subdomains_beginning_with_a_hyphen_or_underscore
|
77
|
+
@account = Account.new :subdomain => '-testing'
|
78
|
+
@account.subdomain_with_underscores = '_testing'
|
79
|
+
assert !@account.save
|
80
|
+
assert @account.errors.on(:subdomain)
|
81
|
+
assert @account.errors.on(:subdomain_with_underscores)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_should_not_save_with_subdomains_ending_with_a_hyphen_or_underscore
|
85
|
+
@account = Account.new :subdomain => 'testing-'
|
86
|
+
@account.subdomain_with_underscores = 'testing_'
|
87
|
+
assert !@account.save
|
88
|
+
assert @account.errors.on(:subdomain)
|
89
|
+
assert @account.errors.on(:subdomain_with_underscores)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_should_not_save_subdomains_with_an_underscore_if_allow_underscores_option_is_false
|
93
|
+
@account = Account.new :subdomain => 'test_ing'
|
94
|
+
assert !@account.save
|
95
|
+
assert @account.errors.on(:subdomain)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_should_save_subdomains_with_an_underscore_if_allow_underscores_option_is_true
|
99
|
+
@account = Account.new :subdomain => 'testing', :subdomain_with_underscores => 'test_ing', :subdomain_with_nil => 'testing', :subdomain_with_blank => 'testing', :subdomain_with_reserved => 'testing'
|
100
|
+
assert @account.save
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_not_save_with_a_reserved_subdomain_from_the_default_list
|
104
|
+
Huberry::ValidatesAsHostnameLabel::RESERVED_HOSTNAMES.each do |hostname|
|
105
|
+
@account = Account.new :subdomain => hostname
|
106
|
+
assert !@account.save
|
107
|
+
assert @account.errors.on(:subdomain)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_should_not_save_with_a_reserved_subdomain_from_a_list
|
112
|
+
@account = Account.new :subdomain_with_reserved => 'funky'
|
113
|
+
assert !@account.save
|
114
|
+
assert @account.errors.on(:subdomain_with_reserved)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_should_not_be_valid_with_a_subdomain_not_from_the_list
|
118
|
+
@account = Account.new :subdomain_with_reserved => 'qqqfds'
|
119
|
+
assert !@account.save
|
120
|
+
assert !@account.errors.on(:subdomain_with_reserved)
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unders-validates_as_hostname_label
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Huber
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-02 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Checks for valid hostname labels
|
17
|
+
email: shuber@huberry.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- CHANGELOG
|
26
|
+
- init.rb
|
27
|
+
- lib/validates_as_hostname_label.rb
|
28
|
+
- MIT-LICENSE
|
29
|
+
- Rakefile
|
30
|
+
- README.markdown
|
31
|
+
has_rdoc: false
|
32
|
+
homepage: http://github.com/shuber/validates_as_hostname_label
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: Checks for valid hostname labels
|
57
|
+
test_files:
|
58
|
+
- test/validates_as_hostname_label_test.rb
|