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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 145ca9cab05675368ca33a513820cdaca5c50747a197635c48455b94464bf93b
|
4
|
+
data.tar.gz: 9dafd02e5e4481a18c05c475eaea0c70c438886efe13d9cf9ed3735f8fb1016f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a99df100cd2ec1b958a3462bd8f314a75687f4e0d0949b80cc4d7dcb8b19a7cd8484d2c9c94444aca8c40bd3c1efff3a0ba8fbee0a9086a52d9615093c436507
|
7
|
+
data.tar.gz: d10314be5e20e31cc370d8245b42ce5c976d9ab4a4f4129e2d5de3627dbcf728d79d9819c54e0c95047b40b5b3107d38d8665a585ee3187045838170ac321404
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.0
|
5
|
+
NewCops: enable
|
6
|
+
|
7
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
8
|
+
Enabled: true
|
9
|
+
|
10
|
+
Layout/LineLength:
|
11
|
+
Max: 150
|
12
|
+
|
13
|
+
Layout/SpaceAroundMethodCallOperator:
|
14
|
+
Enabled: true
|
15
|
+
|
16
|
+
Lint/DeprecatedOpenSSLConstant:
|
17
|
+
Enabled: true
|
18
|
+
|
19
|
+
Lint/MixedRegexpCaptureTypes:
|
20
|
+
Enabled: true
|
21
|
+
|
22
|
+
Lint/RaiseException:
|
23
|
+
Enabled: true
|
24
|
+
|
25
|
+
Lint/StructNewOverride:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
Metrics/BlockLength:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/Documentation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/ExponentialNotation:
|
35
|
+
Enabled: true
|
36
|
+
|
37
|
+
Style/HashEachMethods:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
Style/HashTransformKeys:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
Style/HashTransformValues:
|
44
|
+
Enabled: true
|
45
|
+
|
46
|
+
Style/MixinUsage:
|
47
|
+
Exclude:
|
48
|
+
- 'spec/spec_helper.rb'
|
49
|
+
|
50
|
+
Style/RedundantRegexpCharacterClass:
|
51
|
+
Enabled: true
|
52
|
+
|
53
|
+
Style/RedundantRegexpEscape:
|
54
|
+
Enabled: true
|
55
|
+
|
56
|
+
Style/SlicingWithRange:
|
57
|
+
Enabled: true
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
validates_host
|
data/.travis.yml
CHANGED
@@ -1,4 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
matrix:
|
2
|
+
include:
|
3
|
+
- rvm: 2.0
|
4
|
+
gemfile: gemfiles/Gemfile.rails3
|
5
|
+
- rvm: 2.0
|
6
|
+
gemfile: gemfiles/Gemfile.rails4
|
7
|
+
- rvm: 2.1
|
8
|
+
gemfile: gemfiles/Gemfile.rails3
|
9
|
+
- rvm: 2.1
|
10
|
+
gemfile: gemfiles/Gemfile.rails4
|
11
|
+
- rvm: 2.2
|
12
|
+
gemfile: gemfiles/Gemfile.rails4
|
13
|
+
- rvm: 2.2
|
14
|
+
gemfile: gemfiles/Gemfile.rails5
|
15
|
+
- rvm: 2.3
|
16
|
+
gemfile: gemfiles/Gemfile.rails4
|
17
|
+
- rvm: 2.3
|
18
|
+
gemfile: gemfiles/Gemfile.rails5
|
19
|
+
- rvm: 2.4
|
20
|
+
gemfile: gemfiles/Gemfile.rails4
|
21
|
+
- rvm: 2.4
|
22
|
+
gemfile: gemfiles/Gemfile.rails5
|
23
|
+
- rvm: 2.5
|
24
|
+
gemfile: gemfiles/Gemfile.rails4
|
25
|
+
- rvm: 2.5
|
26
|
+
gemfile: gemfiles/Gemfile.rails5
|
27
|
+
- rvm: 2.5
|
28
|
+
gemfile: gemfiles/Gemfile.rails6
|
29
|
+
- rvm: 2.6
|
30
|
+
gemfile: gemfiles/Gemfile.rails4
|
31
|
+
- rvm: 2.6
|
32
|
+
gemfile: gemfiles/Gemfile.rails5
|
33
|
+
- rvm: 2.6
|
34
|
+
gemfile: gemfiles/Gemfile.rails6
|
35
|
+
- rvm: 2.7
|
36
|
+
gemfile: gemfiles/Gemfile.rails4
|
37
|
+
- rvm: 2.7
|
38
|
+
gemfile: gemfiles/Gemfile.rails5
|
39
|
+
- rvm: 2.7
|
40
|
+
gemfile: gemfiles/Gemfile.rails6
|
41
|
+
- rvm: 3.0
|
42
|
+
gemfile: gemfiles/Gemfile.rails4
|
43
|
+
- rvm: 3.0
|
44
|
+
gemfile: gemfiles/Gemfile.rails6
|
45
|
+
script: rake complete
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
# ValidatesHost
|
1
|
+
# ValidatesHost
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/validates_host.png)](http://badge.fury.io/rb/validates_host) [![Build Status](https://secure.travis-ci.org/plribeiro3000/validates_host.png?branch=master)](http://travis-ci.org/plribeiro3000/validates_host) [![Coverage Status](https://coveralls.io/repos/plribeiro3000/validates_host/badge.png?branch=master)](https://coveralls.io/r/plribeiro3000/validates_host) [![Code Climate](https://codeclimate.com/github/plribeiro3000/validates_host.png)](https://codeclimate.com/github/plribeiro3000/validates_host)
|
4
|
+
|
5
|
+
Validates host attributes and test it in a simple way.
|
6
|
+
Supports ruby 2.0+ and rails 3+
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -18,23 +21,26 @@ Or install it yourself as:
|
|
18
21
|
|
19
22
|
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
Just use like any other validator:
|
22
25
|
|
23
26
|
```ruby
|
24
27
|
class Server < ActiveRecord::Base
|
25
|
-
validates :domain_name, :
|
26
|
-
validates :host_name, :
|
27
|
-
validates :ip, :
|
28
|
+
validates :domain_name, domain_name: true
|
29
|
+
validates :host_name, host_name: true
|
30
|
+
validates :ip, ip: true
|
28
31
|
end
|
29
32
|
|
30
33
|
class Subnet < ActiveRecord::Base
|
31
|
-
validates :value, :
|
34
|
+
validates :value, subnet: true
|
32
35
|
end
|
33
36
|
```
|
34
37
|
|
35
|
-
##
|
38
|
+
## Notes
|
39
|
+
|
40
|
+
It will load a macher to test automatically if the gem is below shoulda-matchers.
|
36
41
|
|
37
|
-
|
42
|
+
## Mantainers
|
43
|
+
[@plribeiro3000](https://github.com/plribeiro3000)
|
38
44
|
|
39
45
|
## Contributing
|
40
46
|
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
3
6
|
|
4
7
|
RSpec::Core::RakeTask.new
|
8
|
+
RuboCop::RakeTask.new
|
5
9
|
|
6
|
-
desc
|
7
|
-
task :
|
10
|
+
desc 'Default Task'
|
11
|
+
task default: [:spec]
|
12
|
+
task complete: %i[spec rubocop]
|
data/lib/validates_host.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
4
|
require 'validates_host/host_name_validator'
|
5
5
|
require 'validates_host/domain_name_validator'
|
6
6
|
require 'validates_host/ip_validator'
|
7
7
|
require 'validates_host/subnet_validator'
|
8
8
|
|
9
|
+
if defined?(::Shoulda)
|
10
|
+
require 'validates_host/require_a_valid_domain_name_matcher'
|
11
|
+
require 'validates_host/require_a_valid_host_name_matcher'
|
12
|
+
require 'validates_host/require_a_valid_ip_matcher'
|
13
|
+
require 'validates_host/require_a_valid_subnet_matcher'
|
14
|
+
end
|
9
15
|
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
module ValidatesHost
|
17
|
+
autoload :Subnet, 'validates_host/subnet'
|
18
|
+
autoload :Ip, 'validates_host/ip'
|
19
|
+
autoload :HostName, 'validates_host/host_name'
|
20
|
+
autoload :DomainName, 'validates_host/domain_name'
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ValidatesHost
|
4
|
+
class DomainName
|
5
|
+
def initialize(domain_name)
|
6
|
+
@domain_name = domain_name
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
return true if @domain_name.blank?
|
11
|
+
|
12
|
+
@domain_name =~ %r{^[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,23 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class DomainNameValidator < 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::DomainName.new(value).valid?
|
5
|
-
end
|
6
|
-
end
|
5
|
+
return if ValidatesHost::DomainName.new(value).valid?
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(domain_name)
|
11
|
-
@domain_name = domain_name
|
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 domain_name
|
20
|
-
@domain_name
|
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
|
@@ -1,23 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class HostNameValidator < 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::HostName.new(value).valid?
|
5
|
-
end
|
6
|
-
end
|
5
|
+
return if ValidatesHost::HostName.new(value).valid?
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(host_name)
|
11
|
-
@host_name = host_name
|
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 host_name
|
20
|
-
@host_name
|
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
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'resolv'
|
4
|
+
|
5
|
+
module ValidatesHost
|
6
|
+
class Ip
|
7
|
+
def initialize(value)
|
8
|
+
@value = value
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
return true if @value.blank?
|
13
|
+
|
14
|
+
@value =~ Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,23 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class IpValidator < 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::Ip.new(value).valid?
|
5
|
-
end
|
6
|
-
end
|
5
|
+
return if ValidatesHost::Ip.new(value).valid?
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(ip)
|
11
|
-
@ip = ip
|
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 ip
|
20
|
-
@ip
|
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
|
@@ -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_domain_name(attribute = :domain_name)
|
7
|
-
|
9
|
+
RequireAValidDomainNameMatcher.new(attribute)
|
8
10
|
end
|
9
11
|
|
10
|
-
class
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
12
|
+
class RequireAValidDomainNameMatcher < ValidationMatcher
|
15
13
|
def description
|
16
14
|
"require #{@attribute} to be a valid domain 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('example')
|
29
27
|
end
|
30
28
|
|
31
29
|
def allows_valid_value
|
32
|
-
allows_value_of(
|
30
|
+
allows_value_of('example.com')
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
37
|
-
end
|
35
|
+
end
|