]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/validate_re.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / validate_re.rb
1 module Puppet::Parser::Functions
2
3   newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args|
4     Perform simple validation of a string against one or more regular
5     expressions. The first argument of this function should be a string to
6     test, and the second argument should be a stringified regular expression
7     (without the // delimiters) or an array of regular expressions.  If none
8     of the regular expressions match the string passed in, compilation will
9     abort with a parse error.
10
11     The following strings will validate against the regular expressions:
12
13         validate_re('one', '^one$')
14         validate_re('one', [ '^one', '^two' ])
15
16     The following strings will fail to validate, causing compilation to abort:
17
18         validate_re('one', [ '^two', '^three' ])
19
20     ENDHEREDOC
21     if args.length != 2 then
22       raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)")
23     end
24
25     msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
26
27     raise Puppet::ParseError, (msg) unless args[1].any? do |re_str|
28       args[0] =~ Regexp.compile(re_str)
29     end
30
31   end
32
33 end