Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/35047-formula-percent-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/spec': minor
'@objectstack/objectql': patch
'@object-ui/fields': patch
'@object-ui/app-shell': patch
---

feat(spec): a formula field can return `percent`

Clause-②: yes (widening)
Metadata-protocol: added

A ratio formula (budget used, gross margin) can now declare `returnType: 'percent'` and read and display as a `percent` field does, instead of a bare decimal beside the stored percentages.

- **The value.** A `percent` formula result is read exactly as a `percent` field reads its stored value: a fraction (`0.25` shows `25%`, so the expression divides and does not multiply by 100), unless the formula declares a `max` above 1, then percentage points (`percentScaleOf`).
- **Its places.** `scale` counts the decimal places of the displayed percentage, as on a `percent` field (absent: 0). The engine rounds a fraction result to `scale + 2` places and a whole percentage to `scale`, so `scale: 1` keeps 0.417 (41.7%) instead of rounding it to 0.4.
- **An existing `percent` field computed from its `expression` rounds the same way.** The engine shares one rounding step for computed values, so a stored `percent` field that computes from an `expression` now keeps `scale + 2` places for a fraction (and `scale` for a whole percentage). Before, its computed fraction was rounded to `scale` itself: at `scale: 1`, 0.417 was stored as 0.4 and read 40.0%; it is now stored 0.417 and reads 41.7%. The change applies to the next computed write; rows written earlier are not rewritten.
- **Its face.** `percentDisplay` applies to a `percent` formula too (bar or plain). The formula renderer formats the result with the percent formatter, and the field and object forms offer `percent` as a return type. Editing the expression in the Studio field inspector keeps a declared `percent` (or `currency`) unless the new expression proves text, a boolean or a date.
- **Declared, never inferred.** No CEL type proves a quotient is a percentage, so authoring keeps a declared `percent` where it would otherwise stamp `number`. Readers that switch on `returnType` (display-name eligibility, filter operator and comparand typing) treat `percent` as a number.

The enum only gains a member: every formula that parsed before parses the same, and nothing stored needs migrating.
5 changes: 3 additions & 2 deletions content/docs/data-modeling/field-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ Calculated field using a CEL expression (see [Expressions](/docs/data-modeling/f
| Property | Type | Default | Description |
|:---|:---|:---|:---|
| `expression` | `string \| Expression` | **required** | CEL calculation expression |
| `returnType` | `'number' \| 'text' \| 'boolean' \| 'date' \| 'currency'` | — | Declared value type of the computed result. `currency`: an amount of money, in the currency the formula's own `currencyConfig` declares (none: dynamic; over record-mode amounts, `record` with their `currencyField`) |
| `returnType` | `'number' \| 'text' \| 'boolean' \| 'date' \| 'currency' \| 'percent'` | — | Declared value type of the computed result. `currency`: an amount of money, in the currency the formula's own `currencyConfig` declares (none: dynamic; over record-mode amounts, `record` with their `currencyField`). `percent`: a ratio, read as a `percent` field reads its stored value — a fraction (`0.25` shows `25%`, so the expression divides and does not multiply by 100) unless the formula declares a `max` above 1, then percentage points; its `scale` is the decimal places of the displayed percentage (absent: 0) and its `percentDisplay` picks the bar or the plain face. Declared, never inferred |

```typescript
{ name: 'total', label: 'Total', type: 'formula', expression: 'record.quantity * record.unit_price', returnType: 'number' }
Expand All @@ -697,7 +697,8 @@ Calculated field using a CEL expression (see [Expressions](/docs/data-modeling/f
evaluates the expression. The form and the grid cell draw it by `returnType`: `number` is
formatted as a number field formats it (to the field's `scale`), `boolean` as Yes / No in the
UI language (a non-boolean reads empty), `date` as the date field's absolute face (not the
grid's relative one), and `text` in a monospace font. With no `returnType`, a number is drawn
grid's relative one), `percent` as a percent field draws it (its `scale`, `max` and
`percentDisplay`), and `text` in a monospace font. With no `returnType`, a number is drawn
as a number and anything else as text; nothing is inferred from the expression.

#### `expression` on a stored field
Expand Down
10 changes: 8 additions & 2 deletions content/docs/data-modeling/formulas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ evaluates (`objectql` computes a formula virtual field from its `expression`
only). Do **not** pass `type: 'currency' | 'number' | …` to `Field.formula` — it
is rejected by the typed `FieldInput` and, untyped, would override `type:'formula'`
so the field silently never computes. To declare what the formula returns, set
**`returnType`** (`'number' | 'text' | 'boolean' | 'date' | 'currency'`). You declare it
**`returnType`** (`'number' | 'text' | 'boolean' | 'date' | 'currency' | 'percent'`). You declare it
yourself — nothing back-fills it — but the agent-callable `validate_expression`
tool reports the type it infers from the expression (`inferredType`) so an AI
author can stamp the matching value. A `'currency'` result is an amount of money
Expand All @@ -141,7 +141,13 @@ and carries its currency the way a currency field does: in the formula's own
record-mode amounts it is `{ currencyMode: 'record', currencyField }`, naming the
same code field). Copy it from
the currency field the formula computes from — `inferFormulaReturn` in
`@objectstack/formula` answers both halves for a formula it can prove is money. Consumers read the declared `returnType`
`@objectstack/formula` answers both halves for a formula it can prove is money. A `'percent'`
result is a ratio, read exactly as a `percent` field reads its stored value: a
fraction (`0.25` shows `25%`, so the expression divides and does not multiply by
100), unless the formula declares a `max` above 1, then percentage points. Its
`scale` counts the decimal places of the displayed percentage (absent: 0), and
`percentDisplay` picks the bar or the plain face. No CEL type proves a quotient
is a percentage, so `'percent'` is only ever declared, never inferred. Consumers read the declared `returnType`
instead of re-parsing the expression (record-title eligibility, for one: a
formula is title-eligible only when its `returnType` is `'text'`, which is what
lets it be *derived* as the record title — an explicit `nameField` pointer is
Expand Down
Loading
Loading