Validating more than one type of data structure at a time

23 views
Skip to first unread message

Jacob McCoy Wade

unread,
Jun 3, 2015, 5:27:15 AM6/3/15
to puppet...@googlegroups.com
I am trying to find out how I can validate more than one type of data structure for a given value in a manifest?  In particular I would like to be able to have either a string or a hash be a valid data structure.
Something like:
if ($myvar != false) { 
validate_string($myvar) || validate_hash($myvar)
}
Does anybody know if this is possible to do?

Craig Dunn

unread,
Jun 3, 2015, 7:00:25 AM6/3/15
to puppet...@googlegroups.com
The validate_* functions are designed to fail the catalog if the
passed variable doesnt match the defined type, sounds like you want
the is_* functions (also from stdlib) which return true or false but
dont fail.

if ( is_string($myvar) or is_hash($myvar) ) {
...
} else {
fail('not a string or a hash')
}


Regards
Craig
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/732c0ec6-d9bb-4f32-9eae-4667037a0e87%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Enviatics | Automation and configuration management
http://www.enviatics.com | @Enviatics
Puppet Training http://www.enviatics.com/training/

Henrik Lindberg

unread,
Jun 3, 2015, 12:57:29 PM6/3/15
to puppet...@googlegroups.com
If you are using version >= 4.0.0 or >= 3.6 with future parser you can
use the type system to do this.

It looks like you want the value to be false (a boolean), a string
(including an empty string), or a hash (including an empty hash, you do
not care what the keys and values are).

if $myvar =~ Variant[Boolean, String, Hash] {
# it is one of those
}

Note that the above does not accept undef values (which is what I think
the intention was).

You can also use the function assert_type if you want a simple assertion
(or an error) rather than just having conditional logic.

assert_type(Variant[Boolean, String, Hash], $myvar)

Hope that helps.
- henrik

> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/732c0ec6-d9bb-4f32-9eae-4667037a0e87%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Jacob McCoy Wade

unread,
Jun 3, 2015, 6:21:35 PM6/3/15
to puppet...@googlegroups.com
This solution works exactly as I was hoping for. Thank you Craig.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CACxdKhG1MujS2StnayWqOnN5bRuGJ2hMwSj-BGz9h6TP-yowyA%40mail.gmail.com.

Jacob McCoy Wade

unread,
Jun 3, 2015, 6:22:21 PM6/3/15
to puppet...@googlegroups.com
This would be a nice elegant solution, however we have not turned the future parser on quite yet. I will certainly keep this function in mind though in the future.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/mkmtig%24f5i%241%40ger.gmane.org.
Reply all
Reply to author
Forward
0 new messages