validates_host 0.3.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -4
- data/.rubocop.yml +57 -0
- data/.ruby-gemset +1 -0
- data/.travis.yml +45 -4
- data/Gemfile +3 -1
- data/README.md +15 -9
- data/Rakefile +9 -4
- data/gemfiles/Gemfile.rails3 +6 -0
- data/gemfiles/Gemfile.rails4 +5 -0
- data/gemfiles/Gemfile.rails5 +5 -0
- data/gemfiles/Gemfile.rails6 +5 -0
- data/lib/validates_host.rb +14 -5
- data/lib/validates_host/domain_name.rb +15 -0
- data/lib/validates_host/domain_name_validator.rb +10 -17
- data/lib/validates_host/host_name.rb +15 -0
- data/lib/validates_host/host_name_validator.rb +10 -17
- data/lib/validates_host/ip.rb +17 -0
- data/lib/validates_host/ip_validator.rb +10 -17
- data/lib/validates_host/{shoulda-matchers/domain_name_matcher.rb → require_a_valid_domain_name_matcher.rb} +9 -11
- data/lib/validates_host/{shoulda-matchers/host_name_matcher.rb → require_a_valid_host_name_matcher.rb} +9 -11
- data/lib/validates_host/{shoulda-matchers/ip_matcher.rb → require_a_valid_ip_matcher.rb} +9 -11
- data/lib/validates_host/{shoulda-matchers/subnet_matcher.rb → require_a_valid_subnet_matcher.rb} +9 -11
- data/lib/validates_host/subnet.rb +15 -0
- data/lib/validates_host/subnet_validator.rb +10 -17
- data/lib/validates_host/version.rb +3 -1
- data/spec/fake_app/server.rb +19 -5
- data/spec/fake_app/subnet.rb +11 -3
- data/spec/shoulda/matchers/active_model/require_a_valid_domain_name_matcher_spec.rb +23 -0
- data/spec/shoulda/matchers/active_model/require_a_valid_host_name_matcher_spec.rb +23 -0
- data/spec/shoulda/matchers/active_model/require_a_valid_ip_matcher_spec.rb +23 -0
- data/spec/shoulda/matchers/active_model/require_a_valid_subnet_matcher_spec.rb +23 -0
- data/spec/spec_helper.rb +11 -8
- data/spec/validates_host/domain_name_validator_spec.rb +23 -22
- data/spec/validates_host/host_name_validator_spec.rb +23 -22
- data/spec/validates_host/ip_validator_spec.rb +38 -22
- data/spec/validates_host/subnet_validator_spec.rb +23 -22
- data/validates_host.gemspec +23 -19
- metadata +84 -95
- data/lib/validates_host/remarkable.rb +0 -4
- data/lib/validates_host/remarkable/domain_name_matcher.rb +0 -29
- data/lib/validates_host/remarkable/host_name_matcher.rb +0 -29
- data/lib/validates_host/remarkable/ip_matcher.rb +0 -29
- data/lib/validates_host/remarkable/subnet_matcher.rb +0 -29
- data/lib/validates_host/shoulda-matchers.rb +0 -4
- data/spec/fake_app/db/migrations/create_servers.rb +0 -13
- data/spec/fake_app/db/migrations/create_subnets.rb +0 -12
- data/spec/validates_host/remarkable/domain_name_matcher_spec.rb +0 -20
- data/spec/validates_host/remarkable/host_name_matcher_spec.rb +0 -20
- data/spec/validates_host/remarkable/ip_matcher_spec.rb +0 -20
- data/spec/validates_host/remarkable/subnet_matcher_spec.rb +0 -20
- data/spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb +0 -20
- data/spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb +0 -20
- data/spec/validates_host/shoulda-matchers/ip_matcher_spec.rb +0 -20
- data/spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb +0 -20
@@ -1,17 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'shoulda-matchers'
|
2
4
|
|
3
5
|
module Shoulda
|
4
6
|
module Matchers
|
5
7
|
module ActiveModel
|
6
8
|
def require_a_valid_host_name(attribute = :host_name)
|
7
|
-
|
9
|
+
RequireAValidHostNameMatcher.new(attribute)
|
8
10
|
end
|
9
11
|
|
10
|
-
class
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
12
|
+
class RequireAValidHostNameMatcher < ValidationMatcher
|
15
13
|
def description
|
16
14
|
"require #{@attribute} to be a valid host name"
|
17
15
|
end
|
@@ -19,19 +17,19 @@ module Shoulda
|
|
19
17
|
def matches?(subject)
|
20
18
|
super(subject)
|
21
19
|
|
22
|
-
disallows_invalid_value
|
20
|
+
disallows_invalid_value && allows_valid_value
|
23
21
|
end
|
24
22
|
|
25
23
|
private
|
26
24
|
|
27
25
|
def disallows_invalid_value
|
28
|
-
disallows_value_of(
|
26
|
+
disallows_value_of('1bd')
|
29
27
|
end
|
30
28
|
|
31
29
|
def allows_valid_value
|
32
|
-
allows_value_of(
|
30
|
+
allows_value_of('bd01')
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
37
|
-
end
|
35
|
+
end
|
@@ -1,17 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'shoulda-matchers'
|
2
4
|
|
3
5
|
module Shoulda
|
4
6
|
module Matchers
|
5
7
|
module ActiveModel
|
6
8
|
def require_a_valid_ip(attribute = :ip)
|
7
|
-
|
9
|
+
RequireAValidIpMatcher.new(attribute)
|
8
10
|
end
|
9
11
|
|
10
|
-
class
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
12
|
+
class RequireAValidIpMatcher < ValidationMatcher
|
15
13
|
def description
|
16
14
|
"require #{@attribute} to be a valid ip"
|
17
15
|
end
|
@@ -19,19 +17,19 @@ module Shoulda
|
|
19
17
|
def matches?(subject)
|
20
18
|
super(subject)
|
21
19
|
|
22
|
-
disallows_invalid_value
|
20
|
+
disallows_invalid_value && allows_valid_value
|
23
21
|
end
|
24
22
|
|
25
23
|
private
|
26
24
|
|
27
25
|
def disallows_invalid_value
|
28
|
-
disallows_value_of(
|
26
|
+
disallows_value_of('10.0.0')
|
29
27
|
end
|
30
28
|
|
31
29
|
def allows_valid_value
|
32
|
-
allows_value_of(
|
30
|
+
allows_value_of('10.10.10.1')
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
37
|
-
end
|
35
|
+
end
|
data/lib/validates_host/{shoulda-matchers/subnet_matcher.rb → require_a_valid_subnet_matcher.rb}
RENAMED
@@ -1,17 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'shoulda-matchers'
|
2
4
|
|
3
5
|
module Shoulda
|
4
6
|
module Matchers
|
5
7
|
module ActiveModel
|
6
8
|
def require_a_valid_subnet(attribute = :value)
|
7
|
-
|
9
|
+
RequireAValidSubnetMatcher.new(attribute)
|
8
10
|
end
|
9
11
|
|
10
|
-
class
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
12
|
+
class RequireAValidSubnetMatcher < ValidationMatcher
|
15
13
|
def description
|
16
14
|
"require #{@attribute} to be a valid subnet"
|
17
15
|
end
|
@@ -19,19 +17,19 @@ module Shoulda
|
|
19
17
|
def matches?(subject)
|
20
18
|
super(subject)
|
21
19
|
|
22
|
-
disallows_invalid_value
|
20
|
+
disallows_invalid_value && allows_valid_value
|
23
21
|
end
|
24
22
|
|
25
23
|
private
|
26
24
|
|
27
25
|
def disallows_invalid_value
|
28
|
-
disallows_value_of(
|
26
|
+
disallows_value_of('10.0.0')
|
29
27
|
end
|
30
28
|
|
31
29
|
def allows_valid_value
|
32
|
-
allows_value_of(
|
30
|
+
allows_value_of('10.10.10.1/28')
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
37
|
-
end
|
35
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ValidatesHost
|
4
|
+
class Subnet
|
5
|
+
def initialize(subnet)
|
6
|
+
@subnet = subnet
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
return true if @subnet.blank?
|
11
|
+
|
12
|
+
@subnet =~ %r{^([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)$}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,23 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class SubnetValidator < ActiveModel::EachValidator
|
2
4
|
def validate_each(record, attribute, value)
|
3
|
-
|
4
|
-
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::Subnet.new(value).valid?
|
5
|
-
end
|
6
|
-
end
|
5
|
+
return if ValidatesHost::Subnet.new(value).valid?
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(subnet)
|
11
|
-
@subnet = subnet
|
12
|
-
end
|
7
|
+
ruby_prior_version_three =
|
8
|
+
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def subnet
|
20
|
-
@subnet
|
10
|
+
if ruby_prior_version_three
|
11
|
+
record.errors.add(attribute, :invalid, options)
|
12
|
+
else
|
13
|
+
record.errors.add(attribute, :invalid, **options)
|
21
14
|
end
|
22
15
|
end
|
23
|
-
end
|
16
|
+
end
|
data/spec/fake_app/server.rb
CHANGED
@@ -1,5 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Server
|
4
|
+
include ActiveModel::Validations
|
5
|
+
include ActiveModel::Conversion
|
6
|
+
extend ActiveModel::Naming
|
7
|
+
|
8
|
+
attr_accessor :domain_name, :host_name, :ip
|
9
|
+
|
10
|
+
validates :domain_name, domain_name: true
|
11
|
+
validates :host_name, host_name: true
|
12
|
+
validates :ip, ip: true
|
13
|
+
|
14
|
+
def initialize(attributes = {})
|
15
|
+
attributes.each do |name, value|
|
16
|
+
send("#{name}=", value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/fake_app/subnet.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Subnet
|
4
|
+
include ActiveModel::Validations
|
5
|
+
include ActiveModel::Conversion
|
6
|
+
extend ActiveModel::Naming
|
7
|
+
|
8
|
+
attr_accessor :id, :value
|
9
|
+
|
10
|
+
validates :value, subnet: true
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Shoulda::Matchers::ActiveModel::RequireAValidDomainNameMatcher do
|
6
|
+
let(:server) { Server.new }
|
7
|
+
|
8
|
+
it 'accepts on domain_name' do
|
9
|
+
expect(server).to require_a_valid_domain_name(:domain_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'accepts without a specified attribute' do
|
13
|
+
expect(server).to require_a_valid_domain_name
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'rejects on host_name' do
|
17
|
+
expect(server).not_to require_a_valid_domain_name(:host_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a description message' do
|
21
|
+
expect(require_a_valid_domain_name.description).to eq('require domain_name to be a valid domain name')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Shoulda::Matchers::ActiveModel::RequireAValidHostNameMatcher do
|
6
|
+
let(:server) { Server.new }
|
7
|
+
|
8
|
+
it 'accepts on host_name' do
|
9
|
+
expect(server).to require_a_valid_host_name(:host_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'accepts without a specified attribute' do
|
13
|
+
expect(server).to require_a_valid_host_name
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'rejects on domain_name' do
|
17
|
+
expect(server).not_to require_a_valid_host_name(:domain_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a description message' do
|
21
|
+
expect(require_a_valid_host_name.description).to eq('require host_name to be a valid host name')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Shoulda::Matchers::ActiveModel::RequireAValidIpMatcher do
|
6
|
+
let(:server) { Server.new }
|
7
|
+
|
8
|
+
it 'accepts on ip' do
|
9
|
+
expect(server).to require_a_valid_ip(:ip)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'accepts without a specified attribute' do
|
13
|
+
expect(server).to require_a_valid_ip
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'rejects on domain_name' do
|
17
|
+
expect(server).not_to require_a_valid_ip(:domain_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a description message' do
|
21
|
+
expect(require_a_valid_ip.description).to eq('require ip to be a valid ip')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Shoulda::Matchers::ActiveModel::RequireAValidSubnetMatcher do
|
6
|
+
let(:subnet) { Subnet.new }
|
7
|
+
|
8
|
+
it 'accepts on value' do
|
9
|
+
expect(subnet).to require_a_valid_subnet(:value)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'accepts without a specified attribute' do
|
13
|
+
expect(subnet).to require_a_valid_subnet
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'rejects on id' do
|
17
|
+
expect(subnet).not_to require_a_valid_subnet(:id)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a description message' do
|
21
|
+
expect(require_a_valid_subnet.description).to eq('require value to be a valid subnet')
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
|
2
|
-
require "rspec"
|
3
|
-
require "active_record"
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
require 'rspec'
|
4
|
+
require 'active_model'
|
5
|
+
require 'coveralls'
|
6
|
+
require 'shoulda-matchers'
|
7
7
|
|
8
|
-
|
8
|
+
Coveralls.wear!
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
require 'validates_host'
|
11
|
+
require 'fake_app/server'
|
12
|
+
require 'fake_app/subnet'
|
13
|
+
|
14
|
+
include Shoulda::Matchers::ActiveModel
|
@@ -1,40 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe DomainNameValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
let(:server) { Server.new }
|
7
|
+
|
8
|
+
context 'when domain_name is invalid' do
|
9
|
+
before do
|
10
|
+
server.domain_name = 'http://'
|
11
|
+
server.valid?
|
9
12
|
end
|
10
13
|
|
11
|
-
it
|
12
|
-
|
14
|
+
it 'sets object as invalid' do
|
15
|
+
expect(server).not_to be_valid
|
13
16
|
end
|
14
17
|
|
15
|
-
it
|
16
|
-
|
17
|
-
@server.errors[:domain_name].should == ['is invalid']
|
18
|
+
it 'sets an error' do
|
19
|
+
expect(server.errors[:domain_name]).to eq(['is invalid'])
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
context
|
22
|
-
before
|
23
|
-
|
23
|
+
context 'when domain_name is valid' do
|
24
|
+
before do
|
25
|
+
server.domain_name = 'example.com'
|
26
|
+
server.valid?
|
24
27
|
end
|
25
28
|
|
26
|
-
it
|
27
|
-
|
29
|
+
it 'sets object as valid' do
|
30
|
+
expect(server).to be_valid
|
28
31
|
end
|
29
32
|
|
30
|
-
it
|
31
|
-
|
32
|
-
@server.errors[:domain_name].should be_blank
|
33
|
+
it 'does not set an error on attribute' do
|
34
|
+
expect(server.errors[:domain_name]).to be_blank
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
it
|
37
|
-
|
38
|
-
@server.valid?.should be_true
|
38
|
+
it 'is valid with a nil value' do
|
39
|
+
expect(server).to be_valid
|
39
40
|
end
|
40
|
-
end
|
41
|
+
end
|
@@ -1,40 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe HostNameValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
let(:server) { Server.new }
|
7
|
+
|
8
|
+
context 'when host_name is invalid' do
|
9
|
+
before do
|
10
|
+
server.host_name = 'http://'
|
11
|
+
server.valid?
|
9
12
|
end
|
10
13
|
|
11
|
-
it
|
12
|
-
|
14
|
+
it 'sets object as invalid' do
|
15
|
+
expect(server).not_to be_valid
|
13
16
|
end
|
14
17
|
|
15
|
-
it
|
16
|
-
|
17
|
-
@server.errors[:host_name].should == ['is invalid']
|
18
|
+
it 'sets an error' do
|
19
|
+
expect(server.errors[:host_name]).to eq(['is invalid'])
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
context
|
22
|
-
before
|
23
|
-
|
23
|
+
context 'when host_name is valid' do
|
24
|
+
before do
|
25
|
+
server.host_name = 'bd01'
|
26
|
+
server.valid?
|
24
27
|
end
|
25
28
|
|
26
|
-
it
|
27
|
-
|
29
|
+
it 'sets object as valid' do
|
30
|
+
expect(server).to be_valid
|
28
31
|
end
|
29
32
|
|
30
|
-
it
|
31
|
-
|
32
|
-
@server.errors[:host_name].should be_blank
|
33
|
+
it 'does not set an error on attribute' do
|
34
|
+
expect(server.errors[:host_name]).to be_blank
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
it
|
37
|
-
|
38
|
-
@server.valid?.should be_true
|
38
|
+
it 'is valid with a nil value' do
|
39
|
+
expect(server).to be_valid
|
39
40
|
end
|
40
|
-
end
|
41
|
+
end
|