Class: IpFormatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/ip_format_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'app/validators/ip_format_validator.rb', line 4

def validate_each(object, attribute, value)
  error_message_block = lambda{ object.errors[attribute] << " must be a valid IPv4 or IPv6 address" }
  begin
    potential_ip = IPAddr.new(value)
    error_message_block.call unless potential_ip.ipv4? || potential_ip.ipv6?
  rescue ArgumentError
    error_message_block.call
  end
end