1 module Puppet::Parser::Functions
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.
11 The following strings will validate against the regular expressions:
13 validate_re('one', '^one$')
14 validate_re('one', [ '^one', '^two' ])
16 The following strings will fail to validate, causing compilation to abort:
18 validate_re('one', [ '^two', '^three' ])
21 if args.length != 2 then
22 raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)")
25 msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
27 raise Puppet::ParseError, (msg) unless args[1].any? do |re_str|
28 args[0] =~ Regexp.compile(re_str)