summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-05-21 22:03:04 +0200
committerGuido Günther <agx@sigxcpu.org>2014-05-22 21:52:11 +0200
commit6b9bc57547e7bc84b17f33a4807f22be6bf91ecb (patch)
treebf2ceb234c0ac842c752222cc5f17a1978390457 /lib
parentc4abfdae1bcc4f24606467e43b738cc412f98741 (diff)
Add validate_nonemptystring_msg
This allows to print a custom message when the string is not valid but only accepts a single argument.
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/functions/validate_nonemptystring_msg.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/validate_nonemptystring_msg.rb b/lib/puppet/parser/functions/validate_nonemptystring_msg.rb
new file mode 100644
index 0000000..f983a55
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_nonemptystring_msg.rb
@@ -0,0 +1,31 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_nonemptystring_msg, :doc => <<-'ENDHEREDOC') do |args|
+ Validate that the passed value is string data and not empty
+ i.e. not undef or ''. Abort catalog compilation if it fails this
+ check and print the message given as second argment.
+
+ The following values will pass:
+
+ $my_string = "one two"
+ validate_nonemptystring_msg($my_string, 'Not a string')
+
+ The following values will fail, causing compilation to abort and print
+ 'Not a string':
+
+ validate_nonemptystring_msg(true. 'Not a string')
+ validate_nonemptystring_msg([ 'some', 'array' ], 'Not a string')
+
+ ENDHEREDOC
+
+ unless args.length == 2 then
+ raise Puppet::ParseError, ("validate_nonemptystring_msg(): wrong number of arguments (#{args.length}; must be 2)")
+ end
+
+ str = args[0]
+ msg = args[1]
+ unless str.is_a?(String) and str != nil and str != '' and str != :undef
+ raise Puppet::ParseError, (msg)
+ end
+ end
+end