Repository is archived
No commit activity in last 3 years
No release in over 3 years
A IPv4 and IPv6 validator for Rails 3 and 4.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
>= 0

Runtime

 Project Readme

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)