Domain name validation through EPP

This commit is contained in:
Martin Lensment 2014-06-30 14:16:58 +03:00
parent b592b973ff
commit d2e30c0c5f
3 changed files with 14 additions and 4 deletions

View file

@ -11,6 +11,11 @@ class Domain < ActiveRecord::Base
def check_availability(domains) def check_availability(domains)
res = [] res = []
domains.each do |x| domains.each do |x|
if !DomainNameValidator.validate(x)
res << {name: x, avail: 0, reason: 'invalid format'}
next
end
res << {name: x, avail: Domain.find_by(name: x) ? 0 : 1} res << {name: x, avail: Domain.find_by(name: x) ? 0 : 1}
end end

View file

@ -1,10 +1,14 @@
class DomainNameValidator < ActiveModel::EachValidator class DomainNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
ok = value =~ /\A[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.ee\z/ unless self.class.validate(value)
ok &&= !(value[2] == '-' && value[3] == '-')
unless ok
record.errors[attribute] << (options[:message] || 'invalid format') record.errors[attribute] << (options[:message] || 'invalid format')
end end
end end
class << self
def validate(value)
ok = value =~ /\A[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.ee\z/
ok &&= !(value[2] == '-' && value[3] == '-')
end
end
end end

View file

@ -9,6 +9,7 @@ xml.epp_head do
@domains.each do |x| @domains.each do |x|
xml.tag!('domain:cd') do xml.tag!('domain:cd') do
xml.tag!('domain:name', x[:name], 'avail' => x[:avail]) xml.tag!('domain:name', x[:name], 'avail' => x[:avail])
xml.tag!('domain:reason', x[:reason]) if x[:reason].present?
end end
end end
end end