Validate an IPAddr string attribute
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config/locales
lib
spec
.coverrails.yml
.gitignore
.travis.yml
CHANGELOG.md
Gemfile
README.md
Rakefile
activemodel-ipaddr_validator.gemspec

README.md

activemodel-ipaddr_validator

Build Status Code Climate Coverage Status Dependency Status

Usage

Add to your Gemfile:

gem 'activemodel-ipaddr_validator'

Run:

bundle install

Then register ipaddr validator to your model:

class MyModel < ActiveRecord::Base
  validates :my_ipaddr_attribute, ipaddr: true
end

instance = MyModel.new

instance.my_ipaddr_attribute = '127.0.0.1'
instance.valid?
#=> true

instance.my_ipaddr_attribute = 'hello world'
instance.valid?
#=> false

instance.my_ipaddr_attribute = IPAddr.new('127.0.0.1')
instance.valid?
#=> true

Custom options

Name Value Default Description
ipv4 Boolean true Accept IPv4.
ipv6 Boolean false Accept IPv6.
array Boolean false Expect an array of strings.
validates :ipv6s_attribute, ipaddr: { array: true, ipv4: false, ipv6: true }
serialize :ipv6s_attribute, Array

Validation outside a model

If you need to validate an IP outside a model, you can do that:

IpaddrValidator.valid?(value, options)