Evaluables are a core concept in mos. They are used in conditions, dynamicParams and dynamicValues.
An evaluable represents an expression that mos. evaluates at runtime. Depending on the type of expression, it can compare values, retrieve data from different sources or transform existing values.
Tutorial video
This video provides an overview of evaluables in mos. It explains the different types of evaluables and their underlying concepts using an age verification example.
Quick guide
There are three types of evaluables:
| Type | Description |
|---|
| Boolean expressions | Compare values and return true or false. |
| Value expressions | Retrieve values from different data sources. |
| Transformation expressions | Process or convert existing values. |
Evaluables are represented as binary trees. This representation makes the order of operations explicit and avoids the need for parentheses or operator precedence rules.
Each node can have zero, one or two child nodes. Child nodes are referenced by the fields left and right. If a node has only one child, it is always assigned to left.
For example, the boolean expression a == b can be represented as follows. The values of a and b are retrieved by the left and right nodes using value expressions.
{
"mode": "equal",
"left": {
"context": "variable",
"field": "a"
},
"right": {
"context": "variable",
"field": "b"
}
}
As shown in this example, different expressions are often used in combination.
Boolean expressions
Boolean expressions compare values and return either true or false.
Boolean expressions are primarily used in conditions. You can also use the trueValue and falseValue fields to convert the boolean result into other values using value expressions.
Modes
The mode field defines which comparison or logical operation is performed.
The following modes are available:
| Mode | Description |
|---|
exists | Checks whether left exists. |
equal, equals | Checks whether left and right are equal. |
notEqual, notEquals | Checks whether left and right are not equal. |
and | And-connection of two conditions. Both must return true for the entire expression to return true. |
or | Or-connection of two conditions. At least one must return true for the entire expression to return true. |
not | Negates the result of the test. Only left is evaluated; right can be ignored. |
startsWith | Checks whether left starts with right. |
endsWith | Checks whether left ends with right. |
greaterOrEqual, greaterOrEquals | Checks whether left is greater than or equal to right. |
greaterThan | Checks whether left is greater than right. |
lessOrEqual, lessOrEquals | Checks whether left is less than or equal to right. |
lessThan | Checks whether left is less than right. |
matchesRegex | Checks whether left matches the regular expression on right. |
Using boolean expressions
Checking a request status code
The following expression checks whether the status code of a request is not 500 (Internal Server Error).
{
"mode": "notEqual",
"left": {
"context": "parameter",
"field": "_statusCode"
},
"right": {
"value": "500"
}
}
Validating an e-mail address
The following expression checks whether the e-mail address entered in the form of the currently active view matches a regular expression for a valid e-mail address.
{
"mode": "matchesRegex",
"left": {
"context": "form",
"field": "mail"
},
"right": {
"value": "(^.+@.+\\..+$)"
}
}
Checking marketing consent
The following expression checks whether the e-mail address is subscribed or whether another consent has been granted. The or mode returns true if at least one of the two conditions is met.
{
"mode": "or",
"left": {
"mode": "equal",
"left": {
"value": true
},
"right": {
"context": "parameter",
"field": "subscriptions.marketingPermission.email.isSubscribed"
}
},
"right": {
"mode": "equal",
"left": {
"value": true
},
"right": {
"context": "parameter",
"field": "preferences.marketingPermission.letter.isConsentGranted"
}
}
}
Value expressions
Value expressions retrieve information from different sources. These include database tables, forms, event parameters, user settings, variables, permissions and basic device or app information.
Contexts
The context field specifies the source from which a value is retrieved. For most contexts, the field field identifies the specific value to retrieve, such as the name of a user setting. Some contexts require additional information. For example, the query context can use a table, query and query parameters to retrieve a value.
The following contexts are available:
| Context | Description | Possible field values |
|---|
app | Retrieves information about the app. | buildNumber, identifier, name, updateAvailable, updateAvailableSince, mosVersion, versionNumber |
audioPlayer | Checks the state of the active audio player. | playing, progress, time, duration, loaded |
capability | Checks whether the device supports a specified capability. | camera, location, nfc, biometry |
date | Retrieves the current date as a unix timestamp. | updateInterval in seconds |
device | Retrieves information about the device. | category, darkModeEnabled, googlePlayServicesMissing, hasDisplayLock, isOnline, locationEnabled, model, networkType, os, systemBuildNumber, systemVersionNumber |
distance | Calculates the distance between two points or between one point and the device location. Points use the notation lat,lon. | left (first point), right (optional second point) |
form | Retrieves the value of the specified field from the form data of the active view. | |
textToSpeech | Checks text-to-speech capabilities. To check whether a language is available, use languageAvailable with the language in right. | languageAvailable |
layerStatus | Retrieves the active state of the specified layer in the active view. You can also retrieve a field from the layer. Separate the layer name and field with a colon, for example gallery:index. | childCount, index (Gallery), indexFromBehind (Gallery), state |
namedForm | Retrieves the value of the specified field from the specified named form. | form:field |
parameter | Retrieves the value of the specified field from the event parameters passed to the event. | |
permission | Checks whether the user has granted the specified permission. | location, locationBackground, push, camera, audio, photoLibrary, calendar, contacts |
query | Retrieves a value using a database query. | table, query, queryParams, field |
screen | Retrieves information about the screen. | dpi, height, orientation, width |
user | Retrieves information about the user. | uid, language |
userSetting | Retrieves the value of the specified field from the user settings. | |
variable | Retrieves a global variable. | left (name of the variable) |
Using value expressions
The following expression retrieves the value of the mail field from the form in the currently active view.
{
"context": "form",
"field": "mail"
}
Retrieving an event parameter
The following expression retrieves the value of subField within myField from the event parameters.
{
"context": "parameter",
"field": "myField.subField"
}
Retrieving a layer state
The following expression retrieves the index of the currently visible element in the gallery layer named gallery.
{
"context": "layerStatus",
"field": "gallery:index"
}
Transformation expressions process or convert values. They can perform arithmetic operations, format dates or manipulate strings.
The transform field specifies which operation to perform. The left and right fields provide the input values.
The following transformations are available:
| Transformation | Description |
|---|
add | Adds left and right. |
sub | Subtracts right from left. |
mul | Multiplies left by right. |
div | Divides left by right. |
mod | Calculates the modulo of left and right. |
parseDate | Parses the string value of left into a date using the format specified in right. |
formatDate | Converts the date value of left into a string using the format specified in right. |
replace | Replaces the string search in left with right. |
replaceRegex | Replaces matches of the regular expression search in left with right. |
append | Appends the string right to left. |
roundToDigits | Rounds the decimal value left to the number of decimal places specified by right. |
subString | Extracts a range from the string left. The range is specified in right in the format from:until. Negative values are supported and index in reverse order. |
split | Splits the string left at the string right. The useTail field determines whether the part after right is returned. |
fallback | Returns right if left is null or an empty string. |
hash | Applies the algorithm specified by right to the value in left. |
addUnit | Adds a unit to left. |
translate | Translates the string in left. |
Subtracting two values
The following expression subtracts the values of two variables.
{
"transform": "sub",
"left": {
"context": "variable",
"field": "totalValue"
},
"right": {
"context": "variable",
"field": "currentBalance"
}
}
Replacing a string
The following expression removes spaces from a string.
{
"transform": "replace",
"left": "{{articlePrice}}",
"right": "",
"search": " "
}
Checking the current hour
The following expression checks whether the current hour is at least 13 in 24-hour format. In other words, it checks whether the time is 13:00 or later.
The formatDate transformation extracts the current hour from the date context. The result is then compared with 13 using the greaterOrEquals mode.
{
"mode": "greaterOrEquals",
"left": {
"transform": "formatDate",
"left": {
"context": "date"
},
"right": {
"value": "HH"
}
},
"right": {
"value": "13"
}
}