domain_name_validator 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +10 -4
- data/Gemfile.lock +1 -1
- data/README.md +107 -13
- data/domain_name_validator.gemspec +1 -1
- data/lib/domain_name_validator/validator.rb +32 -18
- data/lib/domain_name_validator/version.rb +1 -1
- data/spec/domain_name_validator_spec.rb +7 -1
- metadata +4 -4
data/ChangeLog.md
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
-
0.
|
1
|
+
0.3 (2013-06-28)
|
2
2
|
----------------
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
* Added a check, because labels cannot begin with a period.
|
5
|
+
* Updated documentation, plus defined Road Map for future changes.
|
6
|
+
|
7
|
+
0.2 (2013-06-17)
|
8
|
+
----------------
|
9
|
+
|
10
|
+
First version built and deployed as a gem on a real project. Released on
|
11
|
+
Rubygems.org for the first time.
|
6
12
|
|
7
13
|
* Added more RSpec tests for improved test coverage.
|
8
14
|
* Enhanced documentation.
|
9
15
|
|
10
16
|
|
11
|
-
0.1 (2013-06-
|
17
|
+
0.1 (2013-06-10)
|
12
18
|
----------------
|
13
19
|
|
14
20
|
Initial working version of the code, with minimal RSpec tests. Released on
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,48 @@ domain_name_validator
|
|
2
2
|
=====================
|
3
3
|
|
4
4
|
Ever needed to validate a domain name? This gem will validate any domain name
|
5
|
-
represented in ASCII
|
5
|
+
represented in ASCII.
|
6
|
+
|
7
|
+
The scope of this gem is deliberately focused on validating domain names. It
|
8
|
+
simply answers the question: "Is this a real domain name?" Using this command,
|
9
|
+
you can make a realistic assessment about whether you want to store a domain
|
10
|
+
name or URL in your database. This gem will tell you 1) that a domain is or
|
11
|
+
is not valid, and 2) if it's not valid, what the errors are.
|
12
|
+
|
13
|
+
There are other gems and libraries that parse domain names into their various
|
14
|
+
components, or parse URLS, or properly handle Unicode domain names, etc. Use
|
15
|
+
them; many of them are very good at their well-defined roles. But none of the
|
16
|
+
ones that I came across were very good at simply telling me whether a domain
|
17
|
+
names was valid.
|
18
|
+
|
19
|
+
How It Works
|
20
|
+
------------
|
21
|
+
|
22
|
+
To validate a domain name:
|
23
|
+
|
24
|
+
v = DomainNameValidator.new
|
25
|
+
if v.validate('keenertech.com')
|
26
|
+
# Do something
|
27
|
+
end
|
28
|
+
|
29
|
+
What about error messages? If a domain isn't valid, it's often desirable to
|
30
|
+
find out why the domain ewasn't valid. To do this, simply pass an array into
|
31
|
+
the "validate" message as the optional second argument.
|
32
|
+
|
33
|
+
errs = []
|
34
|
+
v = DomainNameValidator.new
|
35
|
+
unless v.validate('keenertech.123', errs)
|
36
|
+
puts("Errors: #{errs.inspect}")
|
37
|
+
end
|
38
|
+
|
39
|
+
This generates the following output:
|
40
|
+
|
41
|
+
Errors: ["The top-level domain (the extension) cannot be numerical"]
|
42
|
+
|
43
|
+
This gem should make it easy to validate domain names.
|
44
|
+
|
45
|
+
About Domain Names
|
46
|
+
------------------
|
6
47
|
|
7
48
|
Domain names provide a unique, memorizable name to represent numerically
|
8
49
|
addressable Internet resources. They also provide a level of abstraction that
|
@@ -10,9 +51,9 @@ allows the underlying Internet address to be changed while still referencing
|
|
10
51
|
a resource by its domain name. The domain name space is managed by the
|
11
52
|
Internet Corporation for Assigned Names and Numbers (ICANN).
|
12
53
|
|
13
|
-
The right-most label of a domain name is referred to as the top-level domain,
|
14
|
-
TLD. A limited set of top-level domain names, and two-character country
|
15
|
-
have been standardized. The Internet Assigned Numbers Authority (IANA)
|
54
|
+
The right-most label of a domain name is referred to as the top-level domain,
|
55
|
+
or TLD. A limited set of top-level domain names, and two-character country
|
56
|
+
codes, have been standardized. The Internet Assigned Numbers Authority (IANA)
|
16
57
|
maintains an annotated list of top-level domains, as well as a list of
|
17
58
|
"special use," or reserved, top-level domain names.
|
18
59
|
|
@@ -21,20 +62,24 @@ maintains an annotated list of top-level domains, as well as a list of
|
|
21
62
|
|
22
63
|
Domain names follow some very detailed rules:
|
23
64
|
|
24
|
-
|
65
|
+
* The maximum length of a domain name is 253 characters.
|
25
66
|
|
26
|
-
|
27
|
-
|
67
|
+
* A domain name is divided into "labels" separated by periods. The maximum
|
68
|
+
number of labels is 127.
|
28
69
|
|
29
|
-
|
70
|
+
* The maximum length of any label within a domain name is 63 characters.
|
30
71
|
|
31
|
-
|
72
|
+
* No label, including TLDs, can begin or end with a dash.
|
32
73
|
|
33
|
-
|
74
|
+
* Top-level domain names cannot be all numeric.
|
34
75
|
|
35
|
-
|
36
|
-
|
37
|
-
|
76
|
+
* The right-most label must be either a recognized TLD or a 2-letter country
|
77
|
+
code. The only exception is for international domain names, for which
|
78
|
+
TLD checking is currently bypassed.
|
79
|
+
|
80
|
+
* Top-level domain names cannot be all numeric.
|
81
|
+
|
82
|
+
* Domain names may not begin with a period.
|
38
83
|
|
39
84
|
|
40
85
|
Internationalized Domain Names
|
@@ -57,6 +102,55 @@ size limit of 63 characters. In the absence of specific guidelines, and because
|
|
57
102
|
I've never actually seen an overly long label, I have chosen to apply the limit
|
58
103
|
irregardless of the presence of the "xn--" prefix within a label.
|
59
104
|
|
105
|
+
Requirements
|
106
|
+
------------
|
107
|
+
|
108
|
+
This is a Ruby gem with no run-time dependencies on anything else. It's only
|
109
|
+
been tested under Ruby 1.9.3, but it should be compatible with all versions of
|
110
|
+
Ruby more recent than Ruby 1.8.6.
|
111
|
+
|
112
|
+
Install
|
113
|
+
-------
|
114
|
+
|
115
|
+
Installation doesn't get much simpler than this:
|
116
|
+
|
117
|
+
gem install domain_name_validator
|
118
|
+
|
119
|
+
Road Map
|
120
|
+
--------
|
121
|
+
|
122
|
+
More types of checks will be added as they are identified. Support for
|
123
|
+
validating top-level domains is also in the works (it's a bit more complex
|
124
|
+
than you might imagine).
|
125
|
+
|
126
|
+
Alternative Gems
|
127
|
+
----------------
|
128
|
+
|
129
|
+
If this domain_name_validator gem does not suit your needs, here are a few
|
130
|
+
recommended gems that may provide you with the additional power (and
|
131
|
+
complexity) that is deliberately absent from this highly focused gem:
|
132
|
+
|
133
|
+
* domain_name - A full-featured gem for parsing/manipulating domain names.
|
134
|
+
* ip_address - For everything you need to do with Ipv4 and Ipv6 addresses.
|
135
|
+
|
136
|
+
|
137
|
+
Author
|
138
|
+
------
|
139
|
+
|
140
|
+
David Keener
|
141
|
+
|
142
|
+
He's is a long-time Rubyist, with extensive experience both in the Internet
|
143
|
+
startup world and government contracting. He is one of the founders of the
|
144
|
+
RubyNation and DevIgnition conferences. He speaks often at technical
|
145
|
+
conferences, and blogs regularly on Internet-related subjects at
|
146
|
+
KeenerTech.com.
|
147
|
+
|
148
|
+
Contributors
|
149
|
+
------------
|
150
|
+
|
151
|
+
Many thanks for the support of General Dynamics and the Department of
|
152
|
+
Homeland Security (DHS).
|
153
|
+
|
60
154
|
YOUR SUPPORT
|
61
155
|
------------
|
62
156
|
|
@@ -5,7 +5,7 @@ require "domain_name_validator/version"
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = "domain_name_validator"
|
7
7
|
gem.version = DomainNameValidator::VERSION
|
8
|
-
gem.date = '2013-06-
|
8
|
+
gem.date = '2013-06-28'
|
9
9
|
gem.platform = Gem::Platform::RUBY
|
10
10
|
gem.authors = ["David Keener"]
|
11
11
|
gem.email = ["dkeener@keenertech.com"]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# The purpose of this class is to provide a simple capability for validating
|
2
|
-
# domain names represented in ASCII
|
3
|
-
#
|
2
|
+
# domain names represented in ASCII, a feature that seems to be missing or
|
3
|
+
# obscured in other more wide-ranging domain-related gems.
|
4
4
|
|
5
5
|
class DomainNameValidator
|
6
6
|
|
@@ -9,14 +9,26 @@ class DomainNameValidator
|
|
9
9
|
MAX_LEVELS = 127
|
10
10
|
MIN_LEVELS = 2
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
ERRS = {
|
13
|
+
:max_domain_size =>
|
14
|
+
'Maximum domain length of 253 exceeded',
|
15
|
+
:max_label_size =>
|
16
|
+
'Maximum domain label length of 63 exceeded',
|
17
|
+
:max_level_size =>
|
18
|
+
'Maximum domain level limit of 127 exceeded',
|
19
|
+
:min_level_size =>
|
20
|
+
'Minimum domain level limit of 2 not achieved',
|
21
|
+
:label_dash_begin =>
|
22
|
+
'No domain label may begin with a dash',
|
23
|
+
:label_dash_end =>
|
24
|
+
'No domain label may end with a dash',
|
25
|
+
:top_numerical =>
|
26
|
+
'The top-level domain (the extension) cannot be numerical',
|
27
|
+
:illegal_chars =>
|
28
|
+
'Domain label contains an illegal character',
|
29
|
+
:illegal_start =>
|
30
|
+
'No domain name may start with a period'
|
31
|
+
}
|
20
32
|
|
21
33
|
# Validates the proper formatting of a normalized domain name, i.e. - a
|
22
34
|
# domain that is represented in ASCII. Thus, international domain names are
|
@@ -29,19 +41,21 @@ class DomainNameValidator
|
|
29
41
|
# 3. The maximum length of any label within a domain name is 63 characters.
|
30
42
|
# 4. No label, including top-level domains, can begin or end with a dash.
|
31
43
|
# 5. Top-level names cannot be all numeric.
|
44
|
+
# 6. A domain name cannot begin with a period.
|
32
45
|
|
33
46
|
def validate(dn, errs = [])
|
34
|
-
errs <<
|
47
|
+
errs << ERRS[:max_domain_size] if dn.size > MAX_DOMAIN_LENGTH
|
35
48
|
parts = dn.split('.')
|
36
|
-
errs <<
|
37
|
-
errs <<
|
49
|
+
errs << ERRS[:max_level_size] if parts.size > MAX_LEVELS
|
50
|
+
errs << ERRS[:min_level_size] if parts.size < MIN_LEVELS
|
38
51
|
parts.each do |p|
|
39
|
-
errs <<
|
40
|
-
errs <<
|
41
|
-
errs <<
|
42
|
-
errs <<
|
52
|
+
errs << ERRS[:max_label_size] if p.size > MAX_LABEL_LENGTH
|
53
|
+
errs << ERRS[:label_dash_begin] if p[0] == '-'
|
54
|
+
errs << ERRS[:label_dash_end] if p[-1] == '-'
|
55
|
+
errs << ERRS[:illegal_chars] unless p.match(/^[a-z0-9\-\_]+$/)
|
43
56
|
end
|
44
|
-
errs <<
|
57
|
+
errs << ERRS[:top_numerical] if parts.last.match(/^[0-9]+$/)
|
58
|
+
errs << ERRS[:illegal_start] if parts.first[0] == '.'
|
45
59
|
|
46
60
|
errs.size == 0 # TRUE if valid, FALSE otherwise
|
47
61
|
end
|
@@ -47,11 +47,17 @@ describe DomainNameValidator do
|
|
47
47
|
response = @validator.validate(domain)
|
48
48
|
response.should be == false
|
49
49
|
end
|
50
|
+
|
51
|
+
it 'should fail when a domain name begins with a period' do
|
52
|
+
domain = ".b.c.com"
|
53
|
+
response = @validator.validate(domain)
|
54
|
+
response.should be == false
|
55
|
+
end
|
50
56
|
end
|
51
57
|
|
52
58
|
describe 'Internationalized (normalized) domain names' do
|
53
59
|
|
54
|
-
it 'should
|
60
|
+
it 'should pass when a normalized international domain name' do
|
55
61
|
domain = "xn--kbenhavn-54.eu"
|
56
62
|
response = @validator.validate(domain)
|
57
63
|
response.should be == true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: domain_name_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &31251708 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *31251708
|
25
25
|
description: Checks the validity of domain names.
|
26
26
|
email:
|
27
27
|
- dkeener@keenertech.com
|