Ask Your Question

How do I use validate_re to check integers?

asked 2013-09-25 04:09:09 -0600

robrwo gravatar image

I am passing port parameters to some custom resources, and would like to validate them as integers, using something like:

define foo(
  $port = 1000,
) {
  require stdlib
  validate_re($port, '^\d+$')


This works for the default value of $port, but whenever I specify a different value for the port, e.g.

foo( 'myfoo': port => 1001 }

it fails with a message that says the port parameter doesn't match the regex.

I don't understand why this is failing.

edit retag flag offensive close merge delete


Same code working fine on my machine. Try printing the $port parameter in the function itself using notify{"${port}":}, it may gives you any clue.

rahul khengare gravatar imagerahul khengare ( 2013-09-25 07:42:11 -0600 )edit

I've tried that. That's why I asked?

robrwo gravatar imagerobrwo ( 2013-09-26 05:34:24 -0600 )edit

And the notify spits out '1001'?

Ancillas gravatar imageAncillas ( 2013-10-19 00:10:01 -0600 )edit

@Ancillas: yes.

robrwo gravatar imagerobrwo ( 2013-10-21 08:32:13 -0600 )edit

I've noticed in the .erb templates that sometimes variables are integers, other times strings. So I'm thinking that may be what's going on with this.

robrwo gravatar imagerobrwo ( 2014-04-11 03:58:43 -0600 )edit

4 Answers

Sort by ยป oldest newest most voted

answered 2013-09-26 01:55:02 -0600

golja gravatar image

updated 2013-10-21 09:17:29 -0600

Or you could use puppetlabs-stdlib is_integer function.

Example of a working code to clarify any doubt:

define foo ($port = 1000) {
  require stdlib
  if is_integer($port) {
    notice("all good integer value")
  else {
    fail("invalid value should be integer")

then if you execute puppet apply -e "foo {'bar': }" you should get:

Notice: Scope(Foo[bar]): all good integer value
edit flag offensive delete link more


But how do I use is_integer to throw an error?

robrwo gravatar imagerobrwo ( 2013-09-26 05:34:53 -0600 )edit

Hmmm if ! is_integer($port) { fail("invalid ${port} value") }

golja gravatar imagegolja ( 2013-09-27 02:14:36 -0600 )edit

That does not work.

robrwo gravatar imagerobrwo ( 2013-10-10 04:12:27 -0600 )edit

with what error ?

golja gravatar imagegolja ( 2013-10-11 19:08:44 -0600 )edit

It fails with the message in fail.

robrwo gravatar imagerobrwo ( 2013-10-14 03:00:02 -0600 )edit

answered 2013-10-18 03:48:02 -0600

updated 2013-10-18 03:48:30 -0600

Just run into the same question, and ended up with this code:

if type($var) != 'integer' { fail("Not an integer) }

type is a function from stdlib, returning a string with the type of the variable.

edit flag offensive delete link more


This is the only answer that works fine for me. Needs a closing double-quote though. The others all fail. Very strange.

Martijn Heemels gravatar imageMartijn Heemels ( 2013-11-06 06:50:22 -0600 )edit

answered 2013-10-21 05:35:37 -0600

Stdlib also has the is_numeric function which allows:

unless is_numeric($foo) {
  fail("Not an integer)
edit flag offensive delete link more


As I've said above, this still fails with an error.

robrwo gravatar imagerobrwo ( 2013-10-21 08:31:26 -0600 )edit

I think I understand the problem: plain, unquoted integers fail, but if I give the integer as a string, it works. Is there a Puppet function for concercing integers into ...(more)

robrwo gravatar imagerobrwo ( 2014-02-26 11:35:25 -0600 )edit

To avoid these issues you should be quoting everything with the proper quotation marks, except boolean values and variables. That way everything is a string by default. The [Puppet Style ...(more)

daff gravatar imagedaff ( 2014-04-01 08:43:57 -0600 )edit

answered 2013-09-26 21:02:58 -0600

GregLarkin gravatar image

Can you pass the port parameter as a string argument to validate_re like so?

define foo(
  $port = 1000,
) {
  require stdlib
  validate_re("${port}", '^\d+$')


I don't know that it will change the behavior, but the validate_re documentation does say (emphasis mine):

Perform simple validation of a string against one or more regular expressions. The first argument of this function should be a string to test, and the second argument should be a stringified regular expression...

I'm not sure why the default value passes through correctly but other values do not. Please post back here with any updates. I'm ... (more)

edit flag offensive delete link more


That doesn't work. I've also tried using if (!is_integer($port)) { fail "port must be an integer, not '${port}'" } and it still fails.

robrwo gravatar imagerobrwo ( 2013-10-09 11:44:29 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2013-09-25 04:09:09 -0600

Seen: 6,291 times

Last updated: Oct 21 '13