Sha256: f6f214790ffde692cbec33cc5d33b07c9943ae41e1cff2db18856cc87b8082b8

Contents?: true

Size: 967 Bytes

Versions: 48

Compression:

Stored size: 967 Bytes

Contents

require 'ipaddr'

module Incline
  ##
  # Validates a string contains a valid IP address.
  class IpAddressValidator < ActiveModel::EachValidator

    ##
    # Validates attributes to determine if the values contain valid IP addresses.
    #
    # Set the :no_mask option to restrict the IP address to singular addresses only.
    def validate_each(record, attribute, value)
      begin
        unless value.blank?
          IPAddr.new(value)
          if options[:no_mask]
            if value =~ /\//
              record.errors[attribute] << (options[:message] || 'must not contain a mask')
            end
          elsif options[:require_mask]
            unless value =~ /\//
              record.errors[attribute] << (options[:message] || 'must contain a mask')
            end
          end
        end
      rescue IPAddr::InvalidAddressError
        record.errors[attribute] << (options[:message] || 'is not a valid IP address')
      end
    end
  end

end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
incline-0.2.6 lib/incline/validators/ip_address_validator.rb
incline-0.2.5 lib/incline/validators/ip_address_validator.rb
incline-0.2.4 lib/incline/validators/ip_address_validator.rb
incline-0.2.3 lib/incline/validators/ip_address_validator.rb
incline-0.1.9 lib/incline/validators/ip_address_validator.rb
incline-0.1.8 lib/incline/validators/ip_address_validator.rb
incline-0.1.7 lib/incline/validators/ip_address_validator.rb
incline-0.1.5 lib/incline/validators/ip_address_validator.rb