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

Example 1: Determine if a value exists

Let's say we receive the following data structure:

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

If we want to perform an operation but aren't sure whether the value exists, we can confirm that it does (or doesn't) with validate.presence:

validate.presence(driver_ids.first, "This error message won't be executed")

Because the value exists, the following is returned:

999

Example 2: Determine if another value exists

We can check the second value using validate.presence:

validate.presence(driver_ids.second, "Value was null!")

Because the second value does not exist, it returns the following with the error message:

"Value was null!"