Class: SubdomainValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/kangal/subdomain.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kangal/subdomain.rb', line 5

def validate_each(object, attribute, value)

  return if options[:allow_nil] && value.nil?
  return if options[:allow_blank] && value.blank?
  return unless value.present?

  reserved_names = %w(www ftp mail pop smtp admin ssl sftp http https)
  reserved_names = options[:reserved] if options[:reserved]

  if reserved_names.include?(value)
    object.errors[attribute] << I18n.t(:reserved, scope: 'kangal.validations.subdomain')
  end

  object.errors[attribute] << I18n.t(:character, scope: 'kangal.validations.subdomain') unless (3..63) === value.length
  object.errors[attribute] << I18n.t(:hyphen, scope: 'kangal.validations.subdomain') unless value =~ /^[^-].*[^-]$/i
  object.errors[attribute] << I18n.t(:alphanumeric, scope: 'kangal.validations.subdomain') unless value =~ /^[a-z0-9\-]*$/i
end