Sha256: f879039b74f24abd2b820a4b819bf5f6a6357dd54879edc7e5e5fe837c634724

Contents?: true

Size: 680 Bytes

Versions: 4

Compression:

Stored size: 680 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

4 entries across 4 versions & 1 rubygems

Version Path
lite-validators-1.3.1 lib/lite/validators/ip_address_validator.rb
lite-validators-1.3.0 lib/lite/validators/ip_address_validator.rb
lite-validators-1.2.0 lib/lite/validators/ip_address_validator.rb
lite-validators-1.1.2 lib/lite/validators/ip_address_validator.rb