5 module Puppet::Parser::Functions
6 newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS
7 Returns true if the string passed to this function is a syntactically correct domain name.
11 if (arguments.size != 1) then
12 raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+
13 "given #{arguments.size} for 1")
16 # Only allow string types
17 return false unless arguments[0].is_a?(String)
19 domain = arguments[0].dup
21 # Limits (rfc1035, 3.1)
26 # Allow ".", it is the top level domain
27 return true if domain == '.'
29 # Remove the final dot, if present.
32 # Check the whole domain
33 return false if domain.empty?
34 return false if domain.length > domain_max_length
36 # The top level domain must be alphabetic if there are multiple labels.
38 return false if domain.include? '.' and not /\.[A-Za-z]+$/.match(domain)
40 # Check each label in the domain
41 labels = domain.split('.')
42 vlabels = labels.each do |label|
43 break if label.length < label_min_length
44 break if label.length > label_max_length
45 break if label[-1..-1] == '-'
46 break if label[0..0] == '-'
47 break unless /^[a-z\d-]+$/i.match(label)
49 return vlabels == labels
54 # vim: set ts=2 sw=2 et :