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.3.14 lib/incline/validators/ip_address_validator.rb
incline-0.3.13 lib/incline/validators/ip_address_validator.rb
incline-0.3.12 lib/incline/validators/ip_address_validator.rb
incline-0.3.11 lib/incline/validators/ip_address_validator.rb
incline-0.3.10 lib/incline/validators/ip_address_validator.rb
incline-0.3.9 lib/incline/validators/ip_address_validator.rb
incline-0.3.8 lib/incline/validators/ip_address_validator.rb
incline-0.3.7 lib/incline/validators/ip_address_validator.rb
incline-0.3.6 lib/incline/validators/ip_address_validator.rb
incline-0.3.5 lib/incline/validators/ip_address_validator.rb
incline-0.3.4 lib/incline/validators/ip_address_validator.rb
incline-0.3.3 lib/incline/validators/ip_address_validator.rb
incline-0.3.2 lib/incline/validators/ip_address_validator.rb
incline-0.3.1 lib/incline/validators/ip_address_validator.rb
incline-0.3.0 lib/incline/validators/ip_address_validator.rb
incline-0.2.36 lib/incline/validators/ip_address_validator.rb
incline-0.2.35 lib/incline/validators/ip_address_validator.rb
incline-0.2.34 lib/incline/validators/ip_address_validator.rb
incline-0.2.28 lib/incline/validators/ip_address_validator.rb
incline-0.2.27 lib/incline/validators/ip_address_validator.rb