# Formulas

Formulas for calculated or computed fields, show/hide formulas and validation formulas all rely on [JSONATA](http://jsonata.org/).

Each row in a Datatable will be represented as a JSON object based on `field label: field text value`.

For example, if we have a row in a Datatable with `Product`, `Price` and `Quantity`, we have a JSON object like:

```
{
   Product:"Widget 1",
   Quantity: 3,
   Price: 5
}
```

You could then do things like:

1. add a `Total` (`formula` field type) and set the formula to `Quantity * Price`
2. add a validation formula on quantity: `Quantity < 10`
3. add cross field validation on `Price` something like `(Quantity * Price) < 100`

Example use custom validation for a phone number regex:

```
Phone ~> /^[+]*[(]?[0-9]{1,4}[)]?[-\s\./0-9]*$/
```

Where field name is `Phone`

![image](https://1771273900-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3HPEoFCyP7Q88dV3CmqD%2Fuploads%2FCLbu448HxrXkwMUrQv10%2Fc1?alt=media)

Example show to `owners and authors` only:

```
user.highestRole = "owner"  or user.highestRole = "author"
```

![image](https://1771273900-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3HPEoFCyP7Q88dV3CmqD%2Fuploads%2FyZmtcbvx7UKsqdJNXi91%2Fc2?alt=media)

If the field is of a type that gives you an array… for example field type is `Checkboxes`, then you can do things like `"A" in Checkboxes` for the formula, if you wanted to check if “A” was selected and use that to show/hide a field.

The default metadata available to formulas includes row/system fields such as `_id`, `_currentUrl`, `_contextRow`, `userID`, `_currentUserID`, `viewers`, `isLocked`, `submitted`, `deviceID`, `appVersion`, `hasConflict`, `clientCounter`, and `clientSubmitted`.

Use [Row Metadata and Relationships](https://docs.buzzy.buzz/rest-api/buzzy-rest-api/rest-api/microapp-data-operations/row-metadata-and-relationships) as the canonical reference for those metadata fields and for `embeddingRowID`. This page keeps the formula-specific usage examples rather than redefining the shared metadata contract.

Additional formula-specific metadata:

* **\_currentDeviceID**: (mobile only) the current device ID from Buzzy's native mobile implementations - useful for device-specific logic\\
* **\_currentAppVersion**: (mobile only) the current app version from Buzzy's native mobile implementations - useful for version-specific logic, e.g., `_currentAppVersion != '1.2.3'` to show update messages\\

Displaying a button based on a valid email you can use a “show” based on regex expression, for example

```
(Email != null ) and $exists(Email) and  ($match($string(Email),/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/i))
```

Where “Email” is an email text field

In addition to the current row's date you can also access some basic information about the currently logged in user:

* **user.name:** the name of the currently logged in user
* **user.\_id**: the Buzzy user's system ID
* **user.email:** the email address of the currently logged in user
* **user.highestRole**: the highest role of the currently logged in user

Note, depending on the specific application you are building, typically, you will create a separate user datatable to capture user custom information.

Please ensure you adhere to both Buzzy's terms and conditions as well as other external restrictions and compliance requirements, like GDPR, HIPAA etc related to other privacy and compliance when exposing and using user related data.

***
