Skip to content

Commit

Permalink
feat(*): Accept IPV6
Browse files Browse the repository at this point in the history
  • Loading branch information
plribeiro3000 committed Jun 7, 2020
1 parent 3cb5e48 commit 00bb9b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/validates_host/ip.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'resolv'

module ValidatesHost
class Ip
def initialize(value)
Expand All @@ -9,7 +11,7 @@ def initialize(value)
def valid?
return true if @value.blank?

@value =~ /^([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}$/
@value =~ Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex)
end
end
end
17 changes: 16 additions & 1 deletion spec/validates_host/ip_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end
end

context 'when ip is valid' do
context 'when ip is IPV4 valid' do
before do
server.ip = '10.10.10.1'
server.valid?
Expand All @@ -35,6 +35,21 @@
end
end

context 'when ip is IPV6 valid' do
before do
server.ip = 'fd92:fe56:b43a:062e:ffff:ffff:ffff:ffff'
server.valid?
end

it 'sets object as valid' do
expect(server).to be_valid
end

it 'does not set an error on attribute' do
expect(server.errors[:ip]).to be_blank
end
end

it 'is valid with a nil value' do
expect(server).to be_valid
end
Expand Down

0 comments on commit 00bb9b5

Please sign in to comment.