Sha256: 2f73a43ff43ee27ee64b64200575386f3fec2d8ebb16b03e6499785b239e2230
Contents?: true
Size: 958 Bytes
Versions: 8
Compression:
Stored size: 958 Bytes
Contents
module ActiveModel module Validations class IpValidator < EachValidator def validate_each(record, attribute, value) record.errors.add(attribute) if value.blank? || !regex.match(value) 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
8 entries across 8 versions & 1 rubygems