Skip to main content
Version: 4.64

Evaluables

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:

TypeDescription
Boolean expressionsCompare values and return true or false.
Value expressionsRetrieve values from different data sources.
Transformation expressionsProcess 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"
}
}
Binary tree representing the boolean expression a == bBinary tree representing the boolean expression a == 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:

ModeDescription
existsChecks whether left exists.
equal, equalsChecks whether left and right are equal.
notEqual, notEqualsChecks whether left and right are not equal.
andAnd-connection of two conditions. Both must return true for the entire expression to return true.
orOr-connection of two conditions. At least one must return true for the entire expression to return true.
notNegates the result of the test. Only left is evaluated; right can be ignored.
startsWithChecks whether left starts with right.
endsWithChecks whether left ends with right.
greaterOrEqual, greaterOrEqualsChecks whether left is greater than or equal to right.
greaterThanChecks whether left is greater than right.
lessOrEqual, lessOrEqualsChecks whether left is less than or equal to right.
lessThanChecks whether left is less than right.
matchesRegexChecks 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": "(^.+@.+\\..+$)"
}
}

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:

ContextDescriptionPossible field values
appRetrieves information about the app.buildNumber, identifier, name, updateAvailable, updateAvailableSince, mosVersion, versionNumber
audioPlayerChecks the state of the active audio player.playing, progress, time, duration, loaded
capabilityChecks whether the device supports a specified capability.camera, location, nfc, biometry
dateRetrieves the current date as a unix timestamp.updateInterval in seconds
deviceRetrieves information about the device.category, darkModeEnabled, googlePlayServicesMissing, hasDisplayLock, isOnline, locationEnabled, model, networkType, os, systemBuildNumber, systemVersionNumber
distanceCalculates 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)
formRetrieves the value of the specified field from the form data of the active view.
textToSpeechChecks text-to-speech capabilities. To check whether a language is available, use languageAvailable with the language in right.languageAvailable
layerStatusRetrieves 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
namedFormRetrieves the value of the specified field from the specified named form.form:field
parameterRetrieves the value of the specified field from the event parameters passed to the event.
permissionChecks whether the user has granted the specified permission.location, locationBackground, push, camera, audio, photoLibrary, calendar, contacts
queryRetrieves a value using a database query.table, query, queryParams, field
screenRetrieves information about the screen.dpi, height, orientation, width
userRetrieves information about the user.uid, language
userSettingRetrieves the value of the specified field from the user settings.
variableRetrieves a global variable.left (name of the variable)

Using value expressions​

Retrieving a form field​

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​

Transformation expressions process or convert values. They can perform arithmetic operations, format dates or manipulate strings.

Transformations​

The transform field specifies which operation to perform. The left and right fields provide the input values.

The following transformations are available:

TransformationDescription
addAdds left and right.
subSubtracts right from left.
mulMultiplies left by right.
divDivides left by right.
modCalculates the modulo of left and right.
parseDateParses the string value of left into a date using the format specified in right.
formatDateConverts the date value of left into a string using the format specified in right.
replaceReplaces the string search in left with right.
replaceRegexReplaces matches of the regular expression search in left with right.
appendAppends the string right to left.
roundToDigitsRounds the decimal value left to the number of decimal places specified by right.
subStringExtracts 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.
splitSplits the string left at the string right. The useTail field determines whether the part after right is returned.
fallbackReturns right if left is null or an empty string.
hashApplies the algorithm specified by right to the value in left.
addUnitAdds a unit to left.
translateTranslates the string in left.

Using transformation expressions​

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"
}
}