Sha256: 35148ad6ba6be95ea7c6da0e4318376ecd1cb07240cc19996544e2c8a659ef1a
Contents?: true
Size: 1018 Bytes
Versions: 15
Compression:
Stored size: 1018 Bytes
Contents
module ActiveModel module Validations class IpValidator < EachValidator def validate_each(record, attribute, value) value_str = value.to_s # might be an IPAddr record.errors.add(attribute) if value_str.blank? || !regex.match(value_str) end def check_validity! raise ArgumentError, "Unknown IP validator format #{options[:format].inspect}" unless [:v4, :v6].include? options[:format] end private def regex case options[:format] when :v4 ipv4_regex when :v6 ipv6_regex end end def ipv4_regex # Extracted from ruby 1.9.2 regex256 = /0 |1(?:[0-9][0-9]?)? |2(?:[0-4][0-9]?|5[0-5]?|[6-9])? |[3-9][0-9]?/x /\A(#{regex256})\.(#{regex256})\.(#{regex256})\.(#{regex256})\z/ end def ipv6_regex require 'resolv' Resolv::IPv6::Regex end end end end
Version data entries
15 entries across 15 versions & 1 rubygems