Class: DomainnameValidator

Inherits:
HostnameValidator show all
Defined in:
lib/validates_hostname.rb

Overview

Validates domain names.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DomainnameValidator

Returns a new instance of DomainnameValidator.



194
195
196
197
198
# File 'lib/validates_hostname.rb', line 194

def initialize(options)
  # The :domainname validator intentionally sets allow_numeric_hostname to true.
  # This behavior cannot be overridden by the user.
  super({ require_valid_tld: true, allow_numeric_hostname: true }.merge(options))
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/validates_hostname.rb', line 200

def validate_each(record, attribute, value)
  super

  return unless value.is_a?(String)

  labels = value.split('.')
  # CHECK 1: if there is only one label it cannot be numeric
  return unless labels.first.match?(/\A\d+\z/) && labels.size == 1

  add_error(record, attribute, :single_numeric_hostname_label)
end