|
|
|
@ -3,6 +3,27 @@
|
|
|
|
|
# @author Kim Nørgaard <jasen@jasen.dk> |
|
|
|
|
class IpValidator < ActiveModel::EachValidator |
|
|
|
|
|
|
|
|
|
IPV4_REGEX = /^ |
|
|
|
|
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} |
|
|
|
|
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) |
|
|
|
|
$/x |
|
|
|
|
|
|
|
|
|
IPV6_REGEX = /^ |
|
|
|
|
( |
|
|
|
|
(([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)) |
|
|
|
|
|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)) |
|
|
|
|
|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){1,7}:) |
|
|
|
|
)$/x |
|
|
|
|
|
|
|
|
|
# @param [Hash] options Options for validation |
|
|
|
|
# @option options [Symbol] :ip_type (:any) The IP address type (:any, :v4 or :v6) |
|
|
|
|
# @see ActiveModel::EachValidator#new |
|
|
|
@ -26,31 +47,14 @@ private
|
|
|
|
|
# @param [String] address the ipv4 address |
|
|
|
|
# @return [Boolean] the validation result |
|
|
|
|
def ipv4?(address) |
|
|
|
|
address =~ /^ |
|
|
|
|
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} |
|
|
|
|
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) |
|
|
|
|
$/x |
|
|
|
|
address =~ IPV4_REGEX |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# Validates IPv6 address |
|
|
|
|
# @param [String] address the ipv6 address |
|
|
|
|
# @return [Boolean] the validation result |
|
|
|
|
def ipv6?(address) |
|
|
|
|
address =~ /^ |
|
|
|
|
( |
|
|
|
|
(([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4}) |
|
|
|
|
|(([0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