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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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')
}
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.
> --
> 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
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.