validate.presence

validate.presence returns a given value if the value is present or an error if the value is not present.

Function category: Validate

Syntax

validate.presence(arg1, arg2)

Arguments

Description

arg1

Datum or reference to a datum.

arg2

Error message to return if arg1 evaluates to null.

Examples

Let's say we receive the following data structure.

{
"driver_ids":
{
"first": 999,
"second": null
}
}

Example 1: Determine if a value exists

If we want to perform an operation but aren't sure whether the value exists, we can check if the first value exists using the following function.

# Confirm if a value exists.
validate.presence(driver_ids.first,
"This error message won't be executed")
# Returns 999
#
# This value indicates the value exists

Example 2: Determine if another value exists

We can check the second value exists using the following function.

# Conform
validate.presence(driver_ids.second,
"Value was null!")
# Returns "Value was null!"
#
# This value does not exist