Class: IpFormatValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- IpFormatValidator
- Defined in:
- app/validators/ip_format_validator.rb
Overview
Validates that value is a valid IPv4 or IPv6 address.
Instance Method Summary collapse
-
#validate_each(object, attribute, value) ⇒ void
Validates that
attribute
'svalue
onobject
is a valid IPv4 or IPv6 address.
Instance Method Details
#validate_each(object, attribute, value) ⇒ void
This method returns an undefined value.
Validates that attribute
's value
on object
is a valid IPv4 or IPv6 address.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/validators/ip_format_validator.rb', line 11 def validate_each(object, attribute, value) = lambda{ object.errors.add 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 .call unless potential_ip.ipv4? || potential_ip.ipv6? rescue ArgumentError .call end end |