Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 validate IPv4 and IPv6 format

I know the validation format for IPv4 and IPv6. But not sure how I can combine them so atleast one format should be true. Here is my validation

validates :src_ip_addr, :presence => true, :uniqueness => true,
            :format => { :with => Resolv::IPv4::Regex, :message => "Not an valid IPv4 format"}

  validates :src_ip_addr, :presence => true, :uniqueness => true,
            :format => { :with => Resolv::IPv6::Regex, :message => "Not an valid IPv6 format"}

How I can combine them so if one format is correct then validation should work. Should fail only if ipv4 and ipv6 format is not correct.

Thanks.

like image 430
user588324 Avatar asked Jun 06 '13 15:06

user588324


People also ask

Is it possible to create your own validations in rails?

However, due to the rich number of methods Rails gives you to interact with validations in general, you can build your own. In addition, when generating a scaffold, Rails will put some ERB into the _form.html.erb that it generates that displays the full list of errors on that model.

What is the default static route in IPv4?

A default static route is a destination IP address 0.0.0.0/0. Ipv4 Config - ( ip route 0.0.0.0 0.0.0.0 <ip for next-hop OR exit interface on the router>) Ipv6 Config - ( ip route ::/0 <ip for next-hop OR exit interface on the router>)

What is the subnet mask of a host route in IPv6?

– (ipv6 route <network-to-route-to>/ <subnet-mask> <exit interface on the router OR next-hop ip address>) A host route is used to route traffic to a specific host NOT Network. The subnet mask in the route will always be all 1’s which means non CIDR is 255.25.255.255 that is /32 for IPv4 and /128 for IPv6.

Where can I find discussion about Ruby on rails documentation?

And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the rubyonrails-docs mailing list .


1 Answers

You can also combine them with Regexp.union:

:format => { :with => Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex) ...
like image 191
flyingjamus Avatar answered Oct 04 '22 08:10

flyingjamus