Sha256: cc314513405db233fe15057a3a84f6b3035929f0e5785babb9345c7c60e46194

Contents?: true

Size: 682 Bytes

Versions: 10

Compression:

Stored size: 682 Bytes

Contents

# frozen_string_literal: true

require 'resolv'

class IpAddressValidator < BaseValidator

  REGEXP ||= {
    ipv4: Resolv::IPv4::Regex,
    ipv6: Resolv::IPv6::Regex
  }.freeze

  def validate_each(record, attribute, value)
    assert_valid_protocol!
    super
  end

  private

  def assert_valid_protocol!
    assert_valid_option!(:protocol, REGEXP.keys.push(:any))
  end

  def protocol
    options[:protocol] || :any
  end

  def valid_attr?
    case protocol
    when :ipv4 then valid_regexp?(:ipv4)
    when :ipv6 then valid_regexp?(:ipv6)
    else valid_regexp?(:ipv4) || valid_regexp?(:ipv6)
    end
  end

  def valid_regexp?(key)
    value.to_s =~ REGEXP[key]
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lite-validators-1.1.1 lib/lite/validators/ip_address_validator.rb
lite-validators-1.1.0 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.7 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.6 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.5 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.4 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.3 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.2 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.1 lib/lite/validators/ip_address_validator.rb
lite-validators-1.0.0 lib/lite/validators/ip_address_validator.rb