Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created March 30, 2016 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/1282959a5b3754f9a53ad4183540e371 to your computer and use it in GitHub Desktop.
Save apeiros/1282959a5b3754f9a53ad4183540e371 to your computer and use it in GitHub Desktop.
require "valaptor/utilities/idn"
module Valaptor
module Utilities
class Domain
def initialize
Valaptor.use "simpleidn"
end
def errors(domain)
result = []
ascii = Idn.to_ascii(domain)
labels = ascii.split(".".freeze, -1)
result << :trailing_dots if domain =~ /\.{2,}\z/ # SimpleIDN removes trailing dots, hence test on domain instead of ascii. A single trailing dot is valid.
result << :domain_too_long if ascii.bytesize > 253
result << :multiple_dots if labels.include?("".freeze)
result << :label_too_long if labels.any? { |label| label.bytesize > 63 }
result << :invalid_label unless labels.all? { |label| label =~ /\A[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9]|[A-Za-z0-9]*)\z/ }
result
end
def valid?(domain)
errors(domain).empty?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment