Class: ActiveModel::Validations::IpAddressValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validators/validates_ip_address.rb

Instance Method Summary collapse

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)


30
31
32
# File 'lib/validators/validates_ip_address.rb', line 30

def check_validity!
  raise ArgumentError, ":only accepts a symbol that can be either :v6 or :v4" if options.key?(:only) && ![:v4, :v6].include?(options[:only])
end

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/validators/validates_ip_address.rb', line 4

def validate_each(record, attribute, value)
  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  valid = false

  case options[:only]
  when :v4
    valid = Validators::Ip.v4?(value.to_s)
    scope = :invalid_ipv4_address
  when :v6
    valid = Validators::Ip.v6?(value.to_s)
    scope = :invalid_ipv6_address
  else
    valid = Validators::Ip.valid?(value.to_s)
    scope = :invalid_ip_address
  end

  unless valid
    record.errors.add(
      attribute, scope,
      :message => options[:message], :value => value
    )
  end
end