diff --git a/lib/ipv4_validator.rb b/lib/ipv4_validator.rb index 67e42ca..a619424 100644 --- a/lib/ipv4_validator.rb +++ b/lib/ipv4_validator.rb @@ -1,19 +1,20 @@ require 'ipaddr' require 'socket' class Ipv4Validator < ActiveModel::EachValidator # Returns true if addr is a valid IPv4 address. - def valid_v4?(addr) + def self.valid?(addr) return false if addr['/'] # IPAddr accepts addr/mask format IPAddr.new(addr, Socket::AF_INET) true rescue IPAddr::AddressFamilyError, IPAddr::InvalidAddressError false end # Add an attribute error if this is not a valid IPv4 address. def validate_each(record, attribute, value) - return if valid_v4?(value) + return if Ipv4Validator.valid?(value) + record.errors[attribute] << 'is not a valid IPv4 address' end end diff --git a/lib/ipv6_validator.rb b/lib/ipv6_validator.rb index cdd1876..e6a67c6 100644 --- a/lib/ipv6_validator.rb +++ b/lib/ipv6_validator.rb @@ -1,22 +1,22 @@ require 'ipaddr' require 'socket' class Ipv6Validator < ActiveModel::EachValidator # Returns true if addr is valid IPv6 address. - def valid_v6?(addr) + def self.valid?(addr) return false if addr['/'] # IPAddr accepts addr/mask format return false if addr['['] return false if addr[']'] IPAddr.new(addr, Socket::AF_INET6) true rescue IPAddr::AddressFamilyError, IPAddr::InvalidAddressError false end # Add an attribute error if this is not a valid IPv4 address. def validate_each(record, attribute, value) - return if valid_v6?(value) + return if Ipv6Validator.valid?(value) record.errors[attribute] << 'is not a valid IPv6 address' end end