Sha256: 175c4aaacf42ae58a726cfd2289e03d89aa77d01f16254517e3a3b2ca39d831e

Contents?: true

Size: 683 Bytes

Versions: 43

Compression:

Stored size: 683 Bytes

Contents

require "ipaddr"

# Validates that attribute is a valid IPv4 or IPv6 address.
class IpFormatValidator < ActiveModel::EachValidator
  # Validates that `attribute`'s `value` on `object` is a valid IPv4 or IPv6 address.
  #
  # @return [void]
  def validate_each(object, attribute, value)
    error_message_block = lambda{ object.errors[attribute] << " must be a valid IPv4 or IPv6 address" }
    begin
      if value.is_a? IPAddr
        potential_ip = value.dup
      else
        potential_ip = IPAddr.new(value)
      end
      
      error_message_block.call unless potential_ip.ipv4? || potential_ip.ipv6?
    rescue ArgumentError
      error_message_block.call
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
metasploit_data_models-4.0.0 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.10 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.9 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.8 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.7 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.6 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.5 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.17 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.4 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.2 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.1 app/validators/ip_format_validator.rb
metasploit_data_models-3.0.0 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.16 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.15 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.14 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.13 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.12 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.11 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.10 app/validators/ip_format_validator.rb
metasploit_data_models-2.0.9 app/validators/ip_format_validator.rb