Skip to content

Commit 5f65682

Browse files
author
Jeff McCune
committed
Merge branch 'feature/master/7657_validate_re'
* feature/master/7657_validate_re: (#7657) Add basic validate_re function
2 parents e757816 + d381a5b commit 5f65682

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Puppet::Parser::Functions
2+
3+
newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args|
4+
Perform simple validation of a string against a regular expression. The second
5+
argument of the function should be a string regular expression (without the //'s)
6+
or an array of regular expressions. If none of the regular expressions in the array
7+
match the string passed in, then an exception will be raised.
8+
9+
Example:
10+
11+
These strings validate against the regular expressions
12+
13+
validate_re('one', '^one$')
14+
validate_re('one', [ '^one', '^two' ])
15+
16+
These strings do NOT validate
17+
18+
validate_re('one', [ '^two', '^three' ])
19+
20+
Jeff McCune <[email protected]>
21+
22+
ENDHEREDOC
23+
if args.length != 2 then
24+
raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)")
25+
end
26+
27+
msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
28+
29+
raise Puppet::ParseError, (msg) unless args[1].any? do |re_str|
30+
args[0] =~ Regexp.compile(re_str)
31+
end
32+
33+
end
34+
35+
end

0 commit comments

Comments
 (0)