validates_host 0.2.0 → 0.3.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.
- data/.travis.yml +4 -0
- data/README.md +19 -3
- data/lib/validates_host.rb +2 -0
- data/lib/validates_host/remarkable.rb +2 -1
- data/lib/validates_host/remarkable/ip_matcher.rb +2 -2
- data/lib/validates_host/remarkable/subnet_matcher.rb +29 -0
- data/lib/validates_host/shoulda-matchers.rb +2 -1
- data/lib/validates_host/shoulda-matchers/subnet_matcher.rb +37 -0
- data/lib/validates_host/subnet_validator.rb +23 -0
- data/lib/validates_host/version.rb +1 -1
- data/spec/fake_app/db/migrations/create_subnets.rb +12 -0
- data/spec/fake_app/subnet.rb +3 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/{validates_host.rb → validates_host}/domain_name_validator_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/host_name_validator_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/ip_validator_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/remarkable/domain_name_matcher_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/remarkable/host_name_matcher_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/remarkable/ip_matcher_spec.rb +0 -0
- data/spec/validates_host/remarkable/subnet_matcher_spec.rb +20 -0
- data/spec/{validates_host.rb → validates_host}/shoulda-matchers/domain_name_matcher_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/shoulda-matchers/host_name_matcher_spec.rb +0 -0
- data/spec/{validates_host.rb → validates_host}/shoulda-matchers/ip_matcher_spec.rb +0 -0
- data/spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb +20 -0
- data/spec/validates_host/subnet_validator_spec.rb +40 -0
- metadata +35 -21
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# ValidatesHost
|
1
|
+
# ValidatesHost [](http://travis-ci.org/plribeiro3000/validates_host)
|
2
2
|
|
3
|
-
|
3
|
+
Rails gem to validate Host Related fields.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,23 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Lets say you have a model that you want to have valid host fields. Just add this to your model:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Server < ActiveRecord::Base
|
25
|
+
validates :domain_name, :domain_name => true
|
26
|
+
validates :host_name, :host_name => true
|
27
|
+
validates :ip, :ip => true
|
28
|
+
end
|
29
|
+
|
30
|
+
class Subnet < ActiveRecord::Base
|
31
|
+
validates :value, :subnet => true
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Test
|
36
|
+
|
37
|
+
This gem has builtin matchers for shoulda-matchers and remarkable.
|
22
38
|
|
23
39
|
## Contributing
|
24
40
|
|
data/lib/validates_host.rb
CHANGED
@@ -4,6 +4,8 @@ require "validates_host/version"
|
|
4
4
|
require 'validates_host/host_name_validator'
|
5
5
|
require 'validates_host/domain_name_validator'
|
6
6
|
require 'validates_host/ip_validator'
|
7
|
+
require 'validates_host/subnet_validator'
|
8
|
+
|
7
9
|
|
8
10
|
#Rspec Matchers
|
9
11
|
require "validates_host/shoulda-matchers" if defined?(::Shoulda)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'remarkable/active_model'
|
2
|
+
|
3
|
+
module Remarkable
|
4
|
+
module ActiveModel
|
5
|
+
module Matchers
|
6
|
+
class SubnetMatcher < Remarkable::ActiveModel::Base
|
7
|
+
arguments :value
|
8
|
+
|
9
|
+
collection_assertions :accept_valid_value?, :reject_valid_value?
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def accept_valid_value?
|
14
|
+
@subject.value = '10.10.10.1/28'
|
15
|
+
@subject.valid?.errors[:value].should == []
|
16
|
+
end
|
17
|
+
|
18
|
+
def reject_valid_value?
|
19
|
+
@subject.value = '01'
|
20
|
+
@subject.valid?.errors[:value].should == ['is invalid']
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def require_a_valid_domain_name(*args, &block)
|
25
|
+
SubnetMatcher.new(*args, &block).spec(self)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
1
|
require 'validates_host/shoulda-matchers/domain_name_matcher'
|
2
2
|
require 'validates_host/shoulda-matchers/host_name_matcher'
|
3
|
-
require 'validates_host/shoulda-matchers/ip_matcher'
|
3
|
+
require 'validates_host/shoulda-matchers/ip_matcher'
|
4
|
+
require 'validates_host/shoulda-matchers/subnet_matcher'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "shoulda-matchers"
|
2
|
+
|
3
|
+
module Shoulda
|
4
|
+
module Matchers
|
5
|
+
module ActiveModel
|
6
|
+
def require_a_valid_subnet(attribute = :value)
|
7
|
+
SubnetMatcher.new(attribute)
|
8
|
+
end
|
9
|
+
|
10
|
+
class SubnetMatcher < ValidationMatcher
|
11
|
+
def initialize(attribute)
|
12
|
+
@attribute = attribute
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
"require #{@attribute} to be a valid subnet"
|
17
|
+
end
|
18
|
+
|
19
|
+
def matches?(subject)
|
20
|
+
super(subject)
|
21
|
+
|
22
|
+
disallows_invalid_value and allows_valid_value
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def disallows_invalid_value
|
28
|
+
disallows_value_of("10.0.0")
|
29
|
+
end
|
30
|
+
|
31
|
+
def allows_valid_value
|
32
|
+
allows_value_of("10.10.10.1/28")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class SubnetValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
4
|
+
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::Subnet.new(value).valid?
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module ValidatesHost
|
9
|
+
class Subnet
|
10
|
+
def initialize(subnet)
|
11
|
+
@subnet = subnet
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
return true if @subnet.blank?
|
16
|
+
@subnet =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}\/(2[4-9]|30)$/
|
17
|
+
end
|
18
|
+
|
19
|
+
def subnet
|
20
|
+
@subnet
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'remarkable/active_model'
|
3
|
+
|
4
|
+
describe Remarkable::ActiveModel::Matchers::SubnetMatcher do
|
5
|
+
before :each do
|
6
|
+
@subnet = Subnet.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should accept on value" do
|
10
|
+
@subnet.should require_a_valid_subnet(:value)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should accept without a specified attribute" do
|
14
|
+
@subnet.should require_a_valid_subnet
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should reject on id" do
|
18
|
+
@subnet.should_not require_a_valid_subnet(:id)
|
19
|
+
end
|
20
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shoulda-matchers'
|
3
|
+
|
4
|
+
describe Shoulda::Matchers::ActiveModel::SubnetMatcher do
|
5
|
+
before :each do
|
6
|
+
@subnet = Subnet.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should accept on value" do
|
10
|
+
@subnet.should require_a_valid_subnet(:value)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should accept without a specified attribute" do
|
14
|
+
@subnet.should require_a_valid_subnet
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should reject on id" do
|
18
|
+
@subnet.should_not require_a_valid_subnet(:id)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SubnetValidator do
|
4
|
+
context "when subnet is invalid" do
|
5
|
+
before :each do
|
6
|
+
@subnet = Subnet.new(:value => "127.0.0")
|
7
|
+
I18n.stub(:t).with(:"activerecord.errors.models.subnet.attributes.value.invalid",
|
8
|
+
:default => :"activerecord.errors.messages.invalid").and_return("is invalid")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should set object as invalid" do
|
12
|
+
@subnet.valid?.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should set an error" do
|
16
|
+
@subnet.valid?
|
17
|
+
@subnet.errors[:value].should == ['is invalid']
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when subnet is valid" do
|
22
|
+
before :each do
|
23
|
+
@subnet = Subnet.new(:value => "10.10.10.1/28")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set object as valid" do
|
27
|
+
@subnet.valid?.should be_true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not set an error on attribute" do
|
31
|
+
@subnet.valid?
|
32
|
+
@subnet.errors[:value].should be_blank
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should be valid with a nil value" do
|
37
|
+
@subnet = Subnet.new(:value => nil)
|
38
|
+
@subnet.valid?.should be_true
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_host
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- .gitignore
|
117
117
|
- .rspec
|
118
118
|
- .rvmrc
|
119
|
+
- .travis.yml
|
119
120
|
- Gemfile
|
120
121
|
- LICENSE
|
121
122
|
- README.md
|
@@ -128,23 +129,31 @@ files:
|
|
128
129
|
- lib/validates_host/remarkable/domain_name_matcher.rb
|
129
130
|
- lib/validates_host/remarkable/host_name_matcher.rb
|
130
131
|
- lib/validates_host/remarkable/ip_matcher.rb
|
132
|
+
- lib/validates_host/remarkable/subnet_matcher.rb
|
131
133
|
- lib/validates_host/shoulda-matchers.rb
|
132
134
|
- lib/validates_host/shoulda-matchers/domain_name_matcher.rb
|
133
135
|
- lib/validates_host/shoulda-matchers/host_name_matcher.rb
|
134
136
|
- lib/validates_host/shoulda-matchers/ip_matcher.rb
|
137
|
+
- lib/validates_host/shoulda-matchers/subnet_matcher.rb
|
138
|
+
- lib/validates_host/subnet_validator.rb
|
135
139
|
- lib/validates_host/version.rb
|
136
140
|
- spec/fake_app/db/migrations/create_servers.rb
|
141
|
+
- spec/fake_app/db/migrations/create_subnets.rb
|
137
142
|
- spec/fake_app/server.rb
|
143
|
+
- spec/fake_app/subnet.rb
|
138
144
|
- spec/spec_helper.rb
|
139
|
-
- spec/validates_host
|
140
|
-
- spec/validates_host
|
141
|
-
- spec/validates_host
|
142
|
-
- spec/validates_host
|
143
|
-
- spec/validates_host
|
144
|
-
- spec/validates_host
|
145
|
-
- spec/validates_host
|
146
|
-
- spec/validates_host
|
147
|
-
- spec/validates_host
|
145
|
+
- spec/validates_host/domain_name_validator_spec.rb
|
146
|
+
- spec/validates_host/host_name_validator_spec.rb
|
147
|
+
- spec/validates_host/ip_validator_spec.rb
|
148
|
+
- spec/validates_host/remarkable/domain_name_matcher_spec.rb
|
149
|
+
- spec/validates_host/remarkable/host_name_matcher_spec.rb
|
150
|
+
- spec/validates_host/remarkable/ip_matcher_spec.rb
|
151
|
+
- spec/validates_host/remarkable/subnet_matcher_spec.rb
|
152
|
+
- spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb
|
153
|
+
- spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb
|
154
|
+
- spec/validates_host/shoulda-matchers/ip_matcher_spec.rb
|
155
|
+
- spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb
|
156
|
+
- spec/validates_host/subnet_validator_spec.rb
|
148
157
|
- validates_host.gemspec
|
149
158
|
homepage: ''
|
150
159
|
licenses: []
|
@@ -160,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
169
|
version: '0'
|
161
170
|
segments:
|
162
171
|
- 0
|
163
|
-
hash:
|
172
|
+
hash: 873921845933518174
|
164
173
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
174
|
none: false
|
166
175
|
requirements:
|
@@ -169,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
178
|
version: '0'
|
170
179
|
segments:
|
171
180
|
- 0
|
172
|
-
hash:
|
181
|
+
hash: 873921845933518174
|
173
182
|
requirements: []
|
174
183
|
rubyforge_project:
|
175
184
|
rubygems_version: 1.8.24
|
@@ -178,14 +187,19 @@ specification_version: 3
|
|
178
187
|
summary: Host Validation Gem
|
179
188
|
test_files:
|
180
189
|
- spec/fake_app/db/migrations/create_servers.rb
|
190
|
+
- spec/fake_app/db/migrations/create_subnets.rb
|
181
191
|
- spec/fake_app/server.rb
|
192
|
+
- spec/fake_app/subnet.rb
|
182
193
|
- spec/spec_helper.rb
|
183
|
-
- spec/validates_host
|
184
|
-
- spec/validates_host
|
185
|
-
- spec/validates_host
|
186
|
-
- spec/validates_host
|
187
|
-
- spec/validates_host
|
188
|
-
- spec/validates_host
|
189
|
-
- spec/validates_host
|
190
|
-
- spec/validates_host
|
191
|
-
- spec/validates_host
|
194
|
+
- spec/validates_host/domain_name_validator_spec.rb
|
195
|
+
- spec/validates_host/host_name_validator_spec.rb
|
196
|
+
- spec/validates_host/ip_validator_spec.rb
|
197
|
+
- spec/validates_host/remarkable/domain_name_matcher_spec.rb
|
198
|
+
- spec/validates_host/remarkable/host_name_matcher_spec.rb
|
199
|
+
- spec/validates_host/remarkable/ip_matcher_spec.rb
|
200
|
+
- spec/validates_host/remarkable/subnet_matcher_spec.rb
|
201
|
+
- spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb
|
202
|
+
- spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb
|
203
|
+
- spec/validates_host/shoulda-matchers/ip_matcher_spec.rb
|
204
|
+
- spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb
|
205
|
+
- spec/validates_host/subnet_validator_spec.rb
|