class ActiveModel::Validations::IpValidator

IPアドレスのフォーマットをチェックする

Constants

PERMITTED_VERSION

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/moca_rlibs/active_model_validators/ip.rb, line 12
def initialize(options)
  options[:version] ||= %i[v4 v6]
  options[:version] = Array(options[:version])
  super
end

Public Instance Methods

validate_each(record, _attribute, value) click to toggle source
# File lib/moca_rlibs/active_model_validators/ip.rb, line 18
def validate_each(record, _attribute, value)
  return if value.blank?

  value = value.presence.to_s
  return if options[:version].include?(:v4) && ::Resolv::IPv4::Regex.match?(value)
  return if options[:version].include?(:v6) && ::Resolv::IPv6::Regex.match?(value)

  record.errors.add('invalid ip format')
end