domain_validator 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/domain_validator.gemspec +27 -0
- data/lib/domain_validator/validator.rb +21 -0
- data/lib/domain_validator/version.rb +3 -0
- data/lib/domain_validator.rb +1 -0
- data/spec/domain_validator_spec.rb +92 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/domain_helpers.rb +17 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 695d7e4c5fb856f0e57def793955382f6f1152e3
|
4
|
+
data.tar.gz: 7617ccbbedc6a2d7b35c8af32df99b9ce8061074
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 23ae0e6a4aaac595331de44150367dc6300757373e680cbda57565b08c6a85d8f47779b9b09cb5d1ffefa6d0bcb99d06643eb3e4df4628d62948bef2428b8588
|
7
|
+
data.tar.gz: ad54f966aff37c5f933550abd42f5f38982a778930077ee054da6ab8dad0c59bb9c4f173951f600769b7576602a37e82bfc19f830cdbb5fc29175ac37af9462b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kyle Dayton
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# DomainValidator
|
2
|
+
|
3
|
+
ActiveModel vaidations for domains
|
4
|
+
|
5
|
+
[](https://travis-ci.org/kdayton-/domain_validator)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'domain_validator'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install domain_validator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add a 'domain' validation to your ActiveModel enabled class
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
validates :domain, :domain => true
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Add some specs (so I don't accidentally break your functionality in future versions)
|
36
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'domain_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "domain_validator"
|
8
|
+
spec.version = DomainValidator::VERSION
|
9
|
+
spec.authors = ["Kyle Dayton"]
|
10
|
+
spec.email = ["kyle@graphicflash.com"]
|
11
|
+
spec.description = %q{Adds ActiveModel validation for domain format.}
|
12
|
+
spec.summary = %q{Adds ActiveModel validation for domain format.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "public_suffix"
|
22
|
+
spec.add_dependency "activemodel"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_model/validator'
|
3
|
+
|
4
|
+
require 'public_suffix'
|
5
|
+
|
6
|
+
module DomainValidator
|
7
|
+
class Validator < ActiveModel::EachValidator
|
8
|
+
|
9
|
+
def validate_each(record, attr_name, value)
|
10
|
+
valid_domain = is_valid_domain?(value)
|
11
|
+
record.errors.add(attr_name, options[:message] || "is invalid") and return unless valid_domain
|
12
|
+
end
|
13
|
+
|
14
|
+
def is_valid_domain?(domain)
|
15
|
+
PublicSuffix.valid? domain
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ActiveModel::Validations::DomainValidator = DomainValidator::Validator
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'domain_validator/validator'
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User < Model
|
4
|
+
validates :domain, :domain => true
|
5
|
+
end
|
6
|
+
|
7
|
+
class UserAllowsNil < Model
|
8
|
+
validates :domain, :domain => {:allow_nil => true}
|
9
|
+
end
|
10
|
+
|
11
|
+
class UserAllowsNilFalse < Model
|
12
|
+
validates :domain, :domain => {:allow_nil => false}
|
13
|
+
end
|
14
|
+
|
15
|
+
class UserAllowsBlank < Model
|
16
|
+
validates :domain, :domain => {:allow_blank => true}
|
17
|
+
end
|
18
|
+
|
19
|
+
class UserAllowsBlankFalse < Model
|
20
|
+
validates :domain, :domain => {:allow_blank => false}
|
21
|
+
end
|
22
|
+
|
23
|
+
class UserWithMessage < Model
|
24
|
+
validates :domain, :domain => {:message => "isn't quite right"}
|
25
|
+
end
|
26
|
+
|
27
|
+
describe DomainValidator do
|
28
|
+
context "with valid domain" do
|
29
|
+
valid_domains.each do |domain|
|
30
|
+
it "#{domain} should be valid" do
|
31
|
+
User.new(:domain => domain).should be_valid
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with invalid domain" do
|
37
|
+
invalid_domains.each do |domain|
|
38
|
+
it "#{domain} should be invalid" do
|
39
|
+
User.new(:domain => domain).should_not be_valid
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "nil domain" do
|
45
|
+
it "should not be valid when :allow_nil option is missing" do
|
46
|
+
User.new(:domain => nil).should_not be_valid
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be valid when :allow_nil option is true" do
|
50
|
+
UserAllowsNil.new(:domain => nil).should be_valid
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not be valid when :allow_nil option is false" do
|
54
|
+
UserAllowsNilFalse.new(:domain => nil).should_not be_valid
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "blank domain" do
|
59
|
+
it "should not be valid when :allow_blank option is missing" do
|
60
|
+
User.new(:domain => " ").should_not be_valid
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be valid when :allow_blank option is true" do
|
64
|
+
UserAllowsBlank.new(:domain => " ").should be_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should not be valid when :allow_blank option is false" do
|
68
|
+
UserAllowsBlankFalse.new(:domain => " ").should_not be_valid
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "error messages" do
|
73
|
+
context "when the :message option is not defined" do
|
74
|
+
subject { User.new :domain => "domain.withinvalidtld" }
|
75
|
+
before { subject.valid? }
|
76
|
+
|
77
|
+
it "should add the default message" do
|
78
|
+
subject.errors[:domain].should include "is invalid"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when the :message option is defined" do
|
83
|
+
subject { UserWithMessage.new :domain => "domain.withinvalidtld" }
|
84
|
+
before { subject.valid? }
|
85
|
+
|
86
|
+
it "should add the customized message" do
|
87
|
+
subject.errors[:domain].should include "isn't quite right"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
require 'domain_validator'
|
5
|
+
require 'rspec'
|
6
|
+
require 'active_model'
|
7
|
+
|
8
|
+
class Model
|
9
|
+
include ActiveModel::Validations
|
10
|
+
|
11
|
+
def initialize(attrs = {})
|
12
|
+
@attributes = attrs
|
13
|
+
end
|
14
|
+
|
15
|
+
def read_attribute_for_validation(key)
|
16
|
+
@attributes[key]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |c|
|
21
|
+
require 'support/domain_helpers'
|
22
|
+
c.extend DomainHelpers
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: domain_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Dayton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: public_suffix
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Adds ActiveModel validation for domain format.
|
84
|
+
email:
|
85
|
+
- kyle@graphicflash.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .travis.yml
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- domain_validator.gemspec
|
97
|
+
- lib/domain_validator.rb
|
98
|
+
- lib/domain_validator/validator.rb
|
99
|
+
- lib/domain_validator/version.rb
|
100
|
+
- spec/domain_validator_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/support/domain_helpers.rb
|
103
|
+
homepage: ''
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.0.3
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Adds ActiveModel validation for domain format.
|
127
|
+
test_files:
|
128
|
+
- spec/domain_validator_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
- spec/support/domain_helpers.rb
|