Sha256: 406186d36ac19363391d77bc45e51ff6bf1315df46996d411a1f7e85da3a9948
Contents?: true
Size: 592 Bytes
Versions: 3
Compression:
Stored size: 592 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 potential_ip = IPAddr.new(value) error_message_block.call unless potential_ip.ipv4? || potential_ip.ipv6? rescue ArgumentError error_message_block.call end end end
Version data entries
3 entries across 3 versions & 1 rubygems