4 module Puppet::Parser::Functions
5 newfunction(:is_domain_name, :type => :rvalue, :doc => <<-DOC
7 **Deprecated:** Returns true if the string passed to this function is
8 a syntactically correct domain name.
11 Returns `true` or `false`
13 > **Note:* **Deprecated** Will be removed in a future version of stdlib. See
14 [`validate_legacy`](#validate_legacy).
18 if arguments.size != 1
19 raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
22 # Only allow string types
23 return false unless arguments[0].is_a?(String)
25 domain = arguments[0].dup
27 # Limits (rfc1035, 3.1)
28 domain_max_length = 255
32 # Allow ".", it is the top level domain
33 return true if domain == '.'
35 # Remove the final dot, if present.
38 # Check the whole domain
39 return false if domain.empty?
40 return false if domain.length > domain_max_length
42 # The top level domain must be alphabetic if there are multiple labels.
44 return false if domain.include?('.') && !%r{\.[A-Za-z]+$}.match(domain)
46 # Check each label in the domain
47 labels = domain.split('.')
48 vlabels = labels.each do |label|
49 break if label.length < label_min_length
50 break if label.length > label_max_length
51 break if label[-1..-1] == '-'
52 break if label[0..0] == '-'
53 break unless %r{^[a-z\d-]+$}i =~ label
55 return vlabels == labels
59 # vim: set ts=2 sw=2 et :