Class: ActiveModel::Validations::DomainValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_model/validations/domain_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DomainValidator

Returns a new instance of DomainValidator.



4
5
6
7
8
9
# File 'lib/active_model/validations/domain_validator.rb', line 4

def initialize(options)
  options[:length]       ||= ::Validator::Domain::LENGTH
  options[:label_length] ||= ::Validator::Domain::LABEL_LENGTH

  super(options)
end

Instance Method Details

#validate_each(record, attr_name, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_model/validations/domain_validator.rb', line 11

def validate_each(record, attr_name, value)
  # do not validate if value is empty
  return if value.nil?

  @validator = ::Validator::Domain.new(value)

  # max domain length
  unless @validator.valid_by_length?(options[:length])
    record.errors.add(attr_name, :'domain.length', options)
  end

  # label is limited to between 1 and 63 octets
  unless @validator.valid_by_label_length?(options[:label_length])
    record.errors.add(attr_name, :'domain.label_length', options)
  end

  # skip proceeding validation if errors
  return unless record.errors.blank?

  unless @validator.valid_by_regexp?
    record.errors.add(attr_name, :'domain.invalid', options)
    return
  end

  # if check_tld = true - check if TLD exists
  if options[:check_tld] != false
    tld = value.split('.').last.upcase
    unless ::Validator::Domain::Tld.exists?(tld)
      record.errors.add(attr_name, :'domain.unknown_tld', options)
    end
  end
end