Class: ActiveModel::Validations::SubdomainValidator

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

Direct Known Subclasses

UsernameValidator

Instance Method Summary collapse

Instance Method Details

#reserved?(subdomain) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/validators/validates_subdomain.rb', line 16

def reserved?(subdomain)
  ::Validators::ReservedSubdomains.reserved?(subdomain, options[:in])
end

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/validators/validates_subdomain.rb', line 6

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

  value = value.to_s

  validate_reserved_subdomain(record, attribute, value)
  validate_format(record, attribute, value)
end

#validate_format(record, attribute, value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/validators/validates_subdomain.rb', line 32

def validate_format(record, attribute, value)
  return if Validators::Hostname.valid_label?(value)

  record.errors.add(
    attribute,
    :"invalid_#{options[:error_name]}",
    message: options[:message],
    value: value
  )
end

#validate_reserved_subdomain(record, attribute, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/validators/validates_subdomain.rb', line 20

def validate_reserved_subdomain(record, attribute, value)
  return unless options.fetch(:reserved, true)
  return unless reserved?(value)

  record.errors.add(
    attribute,
    :"reserved_#{options[:error_name]}",
    message: options[:message],
    value: value
  )
end