[Install](https://docs.puppet.com/puppet/latest/modules_installing.html) the stdlib module to add the functions, facts, and resources of this standard library to Puppet.
Ifyouareauthoringamodulethatdependsonstdlib,besureto[specifydependencies](https://docs.puppet.com/puppet/latest/modules_metadata.html#specifying-dependencies) in your metadata.json.
FileswithspecialcharactersthatarenotvalidUTF-8givetheerrormessage"Invalid byte sequence in UTF-8".Inthiscase,determinethecorrectfileencodingandspecifyitwiththe`encoding`attribute.
>DEPRECATED:Thisfunctionhasbeenreplacedwithabuilt-in[`dig`](https://docs.puppet.com/puppet/latest/function.html#dig) function as of Puppet 4.5.0. Use [`dig44()`](#dig44) for backwards compatibility or use the new version.
Builds a command line string from a given array of strings. Each array item is escaped for Bourne shell. All items are then joined together, with a single space in between. This function behaves the same as Ruby's `Shellwords.shelljoin()` function; see the [Ruby documentation](http://ruby-doc.org/stdlib-2.3.0/libdoc/shellwords/rdoc/Shellwords.html#method-c-shelljoin).
For example:
```puppet
shell_join(['foo bar', 'ba"z'])=>'foo\barba\"z'
```
*Type*: rvalue.
#### `shell_split`
Splits a string into an array of tokens. This function behaves the same as Ruby's `Shellwords.shellsplit()` function; see the [ruby documentation](http://ruby-doc.org/stdlib-2.3.0/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellsplit).
*Example:*
```puppet
shell_split('foo\ bar ba\"z') => ['foo bar', 'ba"z']
Arguments:Astringspecifyingthetimein`strftime`format.SeetheRuby[strftime](https://ruby-doc.org/core-2.1.9/Time.html#method-i-strftime) documentation for details.
For example, `{ "key" => "value" }` becomes `"---\nkey:value\n"`.
*Type*: rvalue.
#### `try_get_value`
**DEPRECATED:** replaced by `dig()`.
Retrieves a value within multiple layers of hashes and arrays.
Arguments:
* A string containing a path, as the first argument. Provide this argument as a string of hash keys or array indexes starting with zero and separated by the path separator character (default "/"). This function goes through the structure by each path component and tries to return the value at the end of the path.
* A default argument as a second argument. This argument is returned if the path is not correct, if no value was found, or if any other error has occurred.
* The path separator character as a last argument.
1. **$data** The data structure we are working with.
2. **'a/b/2'** The path string.
3. **'not_found'** The default value. It will be returned if nothing is found.
(optional, defaults to *`undef`*)
4. **'/'** The path separator character.
(optional, defaults to *'/'*)
*Type*: rvalue.
#### `type3x`
**Deprecated**. This function will be removed in a future release.
Returns a string description of the type of a given value. The type can be a string, array, hash, float, integer, or Boolean. For Puppet 4, use the new type system instead.
Arguments:
* string
* array
* hash
* float
* integer
* Boolean
*Type*: rvalue.
#### `type_of`
This function is provided for backwards compatibility, but the built-in [type() function](https://docs.puppet.com/puppet/latest/reference/function.html#type) provided by Puppet is preferred.
Returns the literal type of a given value. Requires Puppet 4. Useful for comparison of types with `<=` such as in `if type_of($some_value) <= Array[String] { ... }` (which is equivalent to `if $some_value =~ Array[String] { ... }`).
*Type*: rvalue.
#### `union`
Returns a union of two or more arrays, without duplicates.
For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
*Type*: rvalue.
#### `unique`
Removes duplicates from strings and arrays.
For example, `unique("aabbcc")` returns 'abc', and `unique(["a","a","b","b","c","c"])` returns ["a","b","c"].
*Type*: rvalue.
#### `unix2dos`
Returns the DOS version of a given string. Useful when using a File resource with a cross-platform template.
Converts an object, array, or hash of objects to uppercase. Objects to be converted must respond to upcase.
For example, `upcase('abcd')` returns 'ABCD'.
*Type*: rvalue.
*Note:* This function is an implementation of a Ruby class and might not be UTF8 compatible. To ensure compatibility, use this function with Ruby 2.4.0 or greater.
#### `uriescape`
URLEncodes a string or array of strings.
Arguments: Either a single string or an array of strings.
*Type*: rvalue.
*Note:* This function is an implementation of a Ruby class and might not be UTF8 compatible. To ensure compatibility, use this function with Ruby 2.4.0 or greater.
#### `validate_absolute_path`
Validates that a given string represents an absolute path in the filesystem. Works for Windows and Unix style paths.
To raise and display an error message, include the fourth argument:
```puppet
validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas')
```
*Type*: statement.
#### `validate_bool`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Validates that all passed values are either `true` or `false`.
Terminates catalog compilation if any value fails this check.
The following values pass:
```puppet
$iamtrue = true
validate_bool(true)
validate_bool(true, true, false, $iamtrue)
```
The following values fail, causing compilation to terminate:
```puppet
$some_array = [ true ]
validate_bool("false")
validate_bool("true")
validate_bool($some_array)
```
*Type*: statement.
#### `validate_cmd`
Validates a string with an external command.
Arguments:
* The string to test, as the first argument.
* The path to a test command, as the second argument. This argument takes a % as a placeholder for the file path (if no % placeholder is given, defaults to the end of the command). If the command is launched against a tempfile containing the passed string, or returns a non-null value, compilation will terminate with a parse error.
* Optionally, an error message to raise and show to the user, as a third argument.
```puppet
# Defaults to end of path
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
The following values will fail, causing compilation to abort:
~~~
$some_array = [ 'bad_email@/d/efdf.com' ]
validate_email_address($some_array)
~~~
*Type*: statement.
#### `validate_hash`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Validates that all passed values are hash data structures. Terminates catalog compilation if any value fails this check.
The following values will pass:
```puppet
$my_hash = { 'one' => 'two' }
validate_hash($my_hash)
```
The following values will fail, causing compilation to terminate:
```puppet
validate_hash(true)
validate_hash('some_string')
$undefined = `undef`
validate_hash($undefined)
```
*Type*: statement.
#### `validate_integer`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Validates an integer or an array of integers. Terminates catalog compilation if any of the checks fail.
Arguments:
* An integer or an array of integers, as the first argument.
* Optionally, a maximum, as the second argument. (All elements of) the first argument must be equal to or less than this maximum.
* Optionally, a minimum, as the third argument. (All elements of) the first argument must be equal to or greater than than this maximum.
This function fails if the first argument is not an integer or array of integers, or if the second or third arguments are not convertable to an integer. However, if (and only if) a minimum is given, the second argument may be an empty string or `undef`, which serves as a placeholder to ensure the minimum check.
The following values pass:
```puppet
validate_integer(1)
validate_integer(1, 2)
validate_integer(1, 1)
validate_integer(1, 2, 0)
validate_integer(2, 2, 2)
validate_integer(2, '', 0)
validate_integer(2, `undef`, 0)
$foo = `undef`
validate_integer(2, $foo, 0)
validate_integer([1,2,3,4,5], 6)
validate_integer([1,2,3,4,5], 6, 0)
```
* Plus all of the above, but any combination of values passed as strings ('1' or "1").
* Plus all of the above, but with (correct) combinations of negative integer values.
The following values fail, causing compilation to terminate:
```puppet
validate_integer(true)
validate_integer(false)
validate_integer(7.0)
validate_integer({ 1 => 2 })
$foo = `undef`
validate_integer($foo)
validate_integer($foobaridontexist)
validate_integer(1, 0)
validate_integer(1, true)
validate_integer(1, '')
validate_integer(1, `undef`)
validate_integer(1, , 0)
validate_integer(1, 2, 3)
validate_integer(1, 3, 2)
validate_integer(1, 3, true)
```
* Plus all of the above, but any combination of values passed as strings (`false` or "false").
* Plus all of the above, but with incorrect combinations of negative integer values.
* Plus all of the above, but with non-integer items in arrays or maximum / minimum argument.
*Type*: statement.
#### `validate_ip_address`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Validates that the argument is an IP address, regardless of whether it is an IPv4 or an IPv6 address. It also validates IP address with netmask.
The following values will fail, causing compilation to terminate:
```puppet
validate_ip_address(1)
validate_ip_address(true)
validate_ip_address(0.0.0.256)
validate_ip_address('::1', {})
validate_ip_address('0.0.0.0.0')
validate_ip_address('3.3.3')
validate_ip_address('23.43.9.22/64')
validate_ip_address('260.2.32.43')
```
#### `validate_legacy`
Validates a value against both a specified type and a deprecated validation function. Silently passes if both pass, errors if only one validation passes, and fails if both validations return false.
Arguments:
* The type to check the value against,
* The full name of the previous validation function,
* The value to be checked,
* An unspecified number of arguments needed for the previous validation function.
Example:
```puppet
validate_legacy('Optional[String]', 'validate_re', 'Value to be validated', ["."])
```
This function supports updating modules from Puppet 3-style argument validation (using the stdlib `validate_*` functions) to Puppet 4 data types, without breaking functionality for those depending on Puppet 3-style validation.
> Note: This function is compatible only with Puppet 4.4.0 (PE 2016.1) and later.
##### For module users
If you are running Puppet 4, the `validate_legacy` function can help you find and resolve deprecated Puppet 3 `validate_*` functions. These functions are deprecated as of stdlib version 4.13 and will be removed in a future version of stdlib.
Puppet 4 allows improved defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Data types avoid some of the problems with Puppet 3's `validate_*` functions, which were sometimes inconsistent. For example, [validate_numeric](#validate_numeric) unintentionally allowed not only numbers, but also arrays of numbers or strings that looked like numbers.
If you run Puppet 4 and use modules with deprecated `validate_*` functions, you might encounter deprecation messages. The `validate_legacy` function makes these differences visible and makes it easier to move to the clearer Puppet 4 syntax.
The deprecation messages you get can vary, depending on the modules and data that you use. These deprecation messages appear by default only in Puppet 4:
* `Notice: Accepting previously invalid value for target type '<type>'`: This message is informational only. You're using values that are allowed by the new type, but would have been invalid by the old validation function.
* `Warning: This method is deprecated, please use the stdlib validate_legacy function`: The module has not yet upgraded to `validate_legacy`. Use the [deprecation](#deprecation) options to silence warnings for now, or submit a fix with the module's developer. See the information [for module developers](#for-module-developers) below for how to fix the issue.
* `Warning: validate_legacy(<function>) expected <type> value, got <actual type>_`: Your code passes a value that was accepted by the Puppet 3-style validation, but will not be accepted by the next version of the module. Most often, you can fix this by removing quotes from numbers or booleans.
* `Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, validate_legacy(<function>) expected <type> value, got <actual type>`: Your code passes a value that is not acceptable to either the new or the old style validation.
##### For module developers
The `validate_legacy` function helps you move from Puppet 3 style validation to Puppet 4 validation without breaking functionality your module's users depend on.
Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics.
For each parameter of your classes and defined types, choose a new Puppet 4 data type to use. In most cases, the new data type allows a different set of values than the original `validate_*` function. The situation then looks like this:
The code after the validation still has to handle all possible values for now, but users of your code can change their manifests to pass only values that match the new type.
For each `validate_*` function in stdlib, there is a matching `Stdlib::Compat::*` type that allows the appropriate set of values. See the documentation in the `types/` directory in the stdlib source code for caveats.
For example, given a class that should accept only numbers, like this:
Here, the type of `$value` is defined as `Variant[Stdlib::Compat::Numeric, Numeric]`, which allows any `Numeric` (the new type), as well as all values previously accepted by `validate_numeric` (through `Stdlib::Compat::Numeric`).
The call to `validate_legacy` takes care of triggering the correct log or fail message for you. It requires the new type, the previous validation function name, and all arguments to that function.
If your module still supported Puppet 3, this is a breaking change. Update your `metadata.json` requirements section to indicate that your module no longer supports Puppet 3, and bump the major version of your module. With this change, all existing tests for your module should still pass. Create additional tests for the new possible values.
As a breaking change, this is also a good time to call [`deprecation`](#deprecation) for any parameters you want to get rid of, or to add additional constraints on your parameters.
After releasing this version, you can release another breaking change release where you remove all compat types and all calls to `validate_legacy`. At that time, you can also go through your code and remove any leftovers dealing with the previously possible values.
Always note such changes in your CHANGELOG and README.
#### `validate_numeric`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Validates a numeric value, or an array or string of numeric values. Terminates catalog compilation if any of the checks fail.
Arguments:
* A numeric value, or an array or string of numeric values.
* Optionally, a maximum value. (All elements of) the first argument has to be less or equal to this max.
* Optionally, a minimum value. (All elements of) the first argument has to be greater or equal to this min.
This function fails if the first argument is not a numeric (Integer or Float) or an array or string of numerics, or if the second and third arguments are not convertable to a numeric. If, and only if, a minimum is given, the second argument can be an empty string or `undef`, which serves as a placeholder to ensure the minimum check.
For passing and failing usage, see [`validate_integer`](#validate-integer). The same values pass and fail, except that `validate_numeric` also allows floating point values.
*Type*: statement.
#### `validate_re`
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Performs simple validation of a string against one or more regular expressions.
Arguments:
* The string to test, as the first argument. If this argument is not a string, compilation terminates. Use quotes to force stringification.
* A stringified regular expression (without the // delimiters) or an array of regular expressions, as the second argument.
* Optionally, the error message raised and shown to the user, as a third argument.
If none of the regular expressions in the second argument match the string passed in the first argument, compilation terminates with a parse error.
The following strings validate against the regular expressions:
```puppet
validate_re('one', '^one$')
validate_re('one', [ '^one', '^two' ])
```
The following string fails to validate, causing compilation to terminate:
```puppet
validate_re('one', [ '^two', '^three' ])
```
To set the error message:
```puppet
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
Takes one element from first array given and merges corresponding elements from second array given. This generates a sequence of n-element arrays, where *n* is one more than the count of arguments. For example, `zip(['1','2','3'],['4','5','6'])` results in ["1", "4"], ["2", "5"], ["3", "6"].*Type*:rvalue.