domain-validator 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +50 -0
- data/Rakefile +23 -0
- data/domain-validator.gemspec +22 -0
- data/lib/active_model/validations/domain_validator.rb +56 -0
- data/lib/domain-validator.rb +10 -0
- data/lib/domain_validator/locale/en.yml +10 -0
- data/lib/domain_validator/version.rb +3 -0
- data/spec/active_model/validations/domain_validator_spec.rb +120 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/test_classes/domain.rb +7 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c142758ac1ea03510262b73498326e97f437549
|
4
|
+
data.tar.gz: 9d50bf185d5abf8c5b2249cfd51539b1a45049dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91b3deb51a8ef2840798e6d284a035c6d2783d501332266a77d061291ed741b877d005112d49c50e004ba4318f6406d76b16a6c1101e754e4f926278cad59074
|
7
|
+
data.tar.gz: 1d57607c6155d4669c2a79f608d434d5ddac45cf38fd10c18fb0a04ba925d95fac24144e82ae903c37ecff967bf86c0c7cb4bf5ee28bd678dcbf01c736c07c46
|
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
36
|
+
|
37
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Shuttlerock
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Domain Validator [![Travis](https://secure.travis-ci.org/Shuttlerock/domain_validator.png)](http://travis-ci.org/Shuttlerock/domain_validator) [![Code Climate](https://codeclimate.com/github/Shuttlerock/domain_validator/badges/gpa.svg)](https://codeclimate.com/github/Shuttlerock/domain_validator)
|
2
|
+
============================
|
3
|
+
|
4
|
+
This is a ActiveModel validator for domains.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
gem install domain-validator
|
9
|
+
|
10
|
+
Usage
|
11
|
+
-------
|
12
|
+
|
13
|
+
In your models, the gem provides new :domain validator
|
14
|
+
|
15
|
+
class Model < ActiveRecord::Base
|
16
|
+
validates :domain_name, domain: true
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
Domain Validator
|
21
|
+
----------------
|
22
|
+
|
23
|
+
validates :domain_name, domain: true
|
24
|
+
|
25
|
+
validates :domain_name, domain: { message: 'custom message' }
|
26
|
+
|
27
|
+
Localization Tricks
|
28
|
+
-------------------
|
29
|
+
To customize error message, you can use { message: "your custom message" } or simple use Rails localization en.yml file, for instance:
|
30
|
+
|
31
|
+
en:
|
32
|
+
errors:
|
33
|
+
messages:
|
34
|
+
domain:
|
35
|
+
long: "your custom length error message"
|
36
|
+
activemodel:
|
37
|
+
errors:
|
38
|
+
messages:
|
39
|
+
domain:
|
40
|
+
invalid: "custom error message only for activemodel"
|
41
|
+
models:
|
42
|
+
your_model:
|
43
|
+
domain:
|
44
|
+
invalid: "custom error message for YourDomain model"
|
45
|
+
|
46
|
+
|
47
|
+
Copyright
|
48
|
+
---------
|
49
|
+
|
50
|
+
Copyright 2015 Shuttlerock Ltd. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require File.dirname(__FILE__) + '/lib/domain_validator/version'
|
5
|
+
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
task :build => :test do
|
9
|
+
system 'gem build domain-validator.gemspec'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :release => :build do
|
13
|
+
# tag and push
|
14
|
+
system "git tag v#{DomainValidator::VERSION}"
|
15
|
+
system "git push origin --tags"
|
16
|
+
# push the gem
|
17
|
+
system "gem push domain-validator-#{DomainValidator::VERSION}.gem"
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec::Core::RakeTask.new(:test) do |t|
|
21
|
+
t.pattern = 'spec/**/*_spec.rb'
|
22
|
+
t.fail_on_error = true # be explicit
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'lib/domain_validator/version')
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'domain-validator'
|
6
|
+
s.license = 'MIT'
|
7
|
+
s.version = DomainValidator::VERSION
|
8
|
+
s.authors = ['Alexey Vokhmin', 'Dave Perrett', 'Oleg Dashevskii']
|
9
|
+
s.email = ['avokhmin@gmail.com', 'dave@recurser.com', 'be9@be9.ru']
|
10
|
+
s.homepage = 'https://github.com/Shuttlerock/domain_validator'
|
11
|
+
s.summary = 'ActiveModel validations for domains with fully localization support.'
|
12
|
+
s.description = 'ActiveModel validations for domains with fully localization support.'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_runtime_dependency 'activemodel', '>= 3.0.0'
|
20
|
+
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
|
6
|
+
# See: https://gist.github.com/rietta/3836366
|
7
|
+
#
|
8
|
+
# Domain Validator by Frank Rietta
|
9
|
+
# (C) 2012 Rietta Inc. All Rights Reserved.
|
10
|
+
# Licensed under terms of the BSD License.
|
11
|
+
#
|
12
|
+
# To use in a validation, add something like this to your model:
|
13
|
+
#
|
14
|
+
# validates :name, domain: true
|
15
|
+
#
|
16
|
+
class DomainValidator < ActiveModel::EachValidator
|
17
|
+
|
18
|
+
def validate_each(record, attribute, value)
|
19
|
+
return if value.blank?
|
20
|
+
|
21
|
+
# The shortest possible domain name something like Google's g.cn, which is four characters long
|
22
|
+
# The longest possible domain name, as per RFC 1035, RFC 1123, and RFC 2181 is 253 characters
|
23
|
+
if value.length < 4
|
24
|
+
record.errors.add(attribute, :'domain.short', options)
|
25
|
+
elsif value.length > 253
|
26
|
+
record.errors.add(attribute, :'domain.long', options)
|
27
|
+
elsif value !~ /\A([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,6}\z/
|
28
|
+
record.errors.add(attribute, :'domain.invalid', options)
|
29
|
+
elsif value.include?('://')
|
30
|
+
record.errors.add(attribute, :'domain.protocol', options)
|
31
|
+
elsif value.include?('/')
|
32
|
+
record.errors.add(attribute, :'domain.slashes', options)
|
33
|
+
elsif !value.include?('.')
|
34
|
+
record.errors.add(attribute, :'domain.dots', options)
|
35
|
+
else
|
36
|
+
|
37
|
+
# Finally, see if the URI parser recognizes this as a valid URL when given a protocol;
|
38
|
+
# remember, we have already rejected any value that had a protocol specified already
|
39
|
+
valid = begin
|
40
|
+
URI.parse('http://' + value).kind_of?(URI::HTTP)
|
41
|
+
rescue URI::InvalidURIError
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
unless valid
|
46
|
+
record.errors.add(attribute, :'domain.invalid', options)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end # validate_each
|
52
|
+
|
53
|
+
end # DomainValidator
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/i18n'
|
3
|
+
require 'active_support/inflector'
|
4
|
+
|
5
|
+
require 'active_model/validations/domain_validator'
|
6
|
+
|
7
|
+
module DomainValidator
|
8
|
+
end
|
9
|
+
|
10
|
+
I18n.load_path << File.dirname(__FILE__) + '/domain_validator/locale/en.yml'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
domain:
|
5
|
+
short: "is invalid as a bare domain name because it is too short"
|
6
|
+
long: "is invalid as a bare domain name because it is too long"
|
7
|
+
protocol: "is invalid as a bare domain name because it includes a protocol seperator (://)"
|
8
|
+
slashes: "is invalid as a bare domain name because it includes forward slashes (/)"
|
9
|
+
dots: "is invalid as a bare domain name because it doesn't contain any dots (.)"
|
10
|
+
invalid: "is an invalid domain"
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'test_classes/domain'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
module Validations
|
6
|
+
describe DomainValidator do
|
7
|
+
let(:domain) { TestDomain.new }
|
8
|
+
|
9
|
+
describe "validations" do
|
10
|
+
# for blank domain
|
11
|
+
it { expect(domain).to be_valid }
|
12
|
+
|
13
|
+
describe 'valid' do
|
14
|
+
|
15
|
+
it {
|
16
|
+
domain.domain_name = 'this-should-be-valid.com'
|
17
|
+
expect(domain).to be_valid
|
18
|
+
}
|
19
|
+
|
20
|
+
it "could start with letter" do
|
21
|
+
domain.domain_name = "correct.com"
|
22
|
+
expect(domain).to be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
it "could start with number" do
|
26
|
+
domain.domain_name = "4correct.com"
|
27
|
+
expect(domain).to be_valid
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be valid for domain with full length 253 and max length per label 63" do
|
31
|
+
domain.domain_name = "#{'a'*63}.#{'b'*63}.#{'c'*63}.#{'d'*57}.com"
|
32
|
+
expect(domain).to be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
it {
|
36
|
+
domain.domain_name = "#{'w'*63}.com"
|
37
|
+
expect(domain).to be_valid
|
38
|
+
}
|
39
|
+
|
40
|
+
it "should be valid with TLD [.com, .com.ua, .co.uk, .travel, .info, etc...]" do
|
41
|
+
%w(.com .com.ua .co.uk .travel .info).each do |tld|
|
42
|
+
domain.domain_name = "domain#{tld}"
|
43
|
+
expect(domain).to be_valid
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be valid if TLD length is between 2 and 6" do
|
48
|
+
%w(ua museum).each do |tld|
|
49
|
+
domain.domain_name = "domain.#{tld}"
|
50
|
+
expect(domain).to be_valid
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should be valid for custom length and label length" do
|
55
|
+
domain.domain_name = "valid-domain.com"
|
56
|
+
expect(domain).to be_valid
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be valid with unknown TLD" do
|
60
|
+
domain.domain_name = "valid-domain.abcabc"
|
61
|
+
expect(domain).to be_valid
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'invalid' do
|
67
|
+
it "should be invalid if domain length is too short" do
|
68
|
+
domain.domain_name = 'a.b'
|
69
|
+
expect(domain).to_not be_valid
|
70
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.short')
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should be invalid if domain length is too long' do
|
74
|
+
[
|
75
|
+
"not-valid-because-of-length-#{'w'*230}.com",
|
76
|
+
"not-valid-because-of-length-of-label#{'w'*230}.com"
|
77
|
+
].each do |domain_name|
|
78
|
+
domain.domain_name = domain_name
|
79
|
+
expect(domain).to_not be_valid
|
80
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.long')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should be invalid if label length is too long' do
|
85
|
+
domain.domain_name = "#{'w'*64}.com"
|
86
|
+
expect(domain).to_not be_valid
|
87
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.invalid')
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should be invalid if consists of special symbols (&, _, {, ], *, etc)" do
|
91
|
+
domain.domain_name = "&_{]*.com"
|
92
|
+
expect(domain).to_not be_valid
|
93
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.invalid')
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should not start with special symbol" do
|
97
|
+
domain.domain_name = "_incorrect.com"
|
98
|
+
expect(domain).to_not be_valid
|
99
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.invalid')
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should not be valid with TLD length more than 6" do
|
103
|
+
domain.domain_name = "domain.abcabcd"
|
104
|
+
expect(domain).to_not be_valid
|
105
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.invalid')
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should not be valid if TLD consists of numbers or special symbols (&, _, {, ], *, etc)" do
|
109
|
+
%w(2 & _ { ] *).each do |tld|
|
110
|
+
domain.domain_name = "domain.a#{tld}"
|
111
|
+
expect(domain).to_not be_valid
|
112
|
+
expect(domain.errors[:domain_name]).to include I18n.t(:'errors.messages.domain.invalid')
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: domain-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Vokhmin
|
8
|
+
- Dave Perrett
|
9
|
+
- Oleg Dashevskii
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2015-08-21 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activemodel
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.0.0
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: activesupport
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 3.0.0
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: ActiveModel validations for domains with fully localization support.
|
58
|
+
email:
|
59
|
+
- avokhmin@gmail.com
|
60
|
+
- dave@recurser.com
|
61
|
+
- be9@be9.ru
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- ".gitignore"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- domain-validator.gemspec
|
73
|
+
- lib/active_model/validations/domain_validator.rb
|
74
|
+
- lib/domain-validator.rb
|
75
|
+
- lib/domain_validator/locale/en.yml
|
76
|
+
- lib/domain_validator/version.rb
|
77
|
+
- spec/active_model/validations/domain_validator_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/test_classes/domain.rb
|
80
|
+
homepage: https://github.com/Shuttlerock/domain_validator
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.4.7
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: ActiveModel validations for domains with fully localization support.
|
104
|
+
test_files:
|
105
|
+
- spec/active_model/validations/domain_validator_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/test_classes/domain.rb
|