Project

ip_format

0.0
No commit activity in last 3 years
No release in over 3 years
Validates IP address format using the resolv library.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0

Runtime

 Project Readme

IpFormat

This is a gem that leverages the Resolv library to validate IP addresses. This can handle both, IPV4 and IPV6.

Installation

Add this line to your application's Gemfile:

gem 'ip_format'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ip_format

Usage

In order to validate an attribute, use the validates keyword:

class Device < ActiveRecord::Base

  # ...

  validates :ip, ip_format: true

  # ...

end

Now the ip attribute will be validated:

Device.new(ip: 'invalidip').valid?        # => false
Device.new(ip: '192.68.0.1').valid? # => true

Also, the model in question doesn't need to inherit from ActiveRecord::Base, you only need to include ActiveModel::Validations in your class:

require 'ip_format'

class Awesome
  include ActiveModel::Validations
  attr_accessor :ip
  validates :ip, ip_format: true
end

awesome = Awesome.new

awesome.ip = "fde4:8dba:82e1::"
awesome.valid? # => true

awesome.ip = "invalidip"
awesome.valid? # => false

Check format for only IPV4 or IPV6

There are two other validators, ipv4_format and ipv6_format. They can be called in place of ip_format if you want the IP address to be validated specifically against IPV4 or IPV6.

IPV6 Format

The below example validates against the IPV6 format regex defined by the Resolv gem.

require 'ip_format'

class Awesome
  include ActiveModel::Validations
  attr_accessor :ip
  validates :ip, ipv6_format: true
end

awesome = Awesome.new

awesome.ip = "fde4:8dba:82e1::"
awesome.valid? # => true

awesome.ip = "192.68.0.1"
awesome.valid? # => false

IPV4 Format

The below example validates against the IPV4 format regex defined by the Resolv gem.

require 'ip_format'

class Awesome
  include ActiveModel::Validations
  attr_accessor :ip
  validates :ip, ipv4_format: true
end

awesome = Awesome.new

awesome.ip = "fde4:8dba:82e1::"
awesome.valid? # => false

awesome.ip = "192.68.0.1"
awesome.valid? # => true

Why?

IP Format uses Resolv, which comes with the Ruby standard lib. This ensures that a new dependency isn't introduced, keeping away the bloat that is sometimes involved with adding gems that have a long line of dependencies.

The ipaddress gem is a great option if you need a more robust solution with subnetting and prefix information. If that functionality isn't a requirement, then the IP Format gem is what you're looking for; it's as simple and lightweight as can be (assuming you're already using ActiveModel).

Contributing

  1. Fork it ( https://github.com/a10networks/ip_format/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request