-
-
Save apeiros/1282959a5b3754f9a53ad4183540e371 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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