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
15 changes: 15 additions & 0 deletions .changeset/37909-rule-message-skills-docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/skills': patch
'@objectstack/spec': patch
---

docs(skills): the objectstack-data validation rules teach a rule message's `{{ }}` holes

Clause-②: no
Metadata-protocol: none

`skills/objectstack-data/rules/validation.md` gains § Messages — Record Values in Holes, teaching what the refusal sentence does since validation rule messages were rendered at run time: a hole names one of the object's own fields under `record` (the judged record) or `previous` (the stored row before an update, empty on insert), with at most one whitelisted formatter; a bare `| currency` formats in the organization's default currency, and with none set that hole renders empty while the server log names the setting (Setup → Localization); a field holding its own currency names the code (`| currency:EUR`) or uses `| number:2`; a field the person whose write was refused cannot read, or sees masked, renders empty, `default:` included; a translation at `objects.<object>._validations.<rule>.message` takes the same holes and is filled first; the sentence is plain text.

§ Incorrect vs Correct is gone: its one example (a rule with no `events` runs on insert and update; scope it with `events: ['update']`) restated § Events.

The upgrade entry `validation-rule-message-template-holes` (`@objectstack/spec`) names the same field before and after in its example: `Total {total_price} is below {received_amount}` becomes `Total {{ record.total_price | currency }} is below {{ record.received_amount | currency }}`.
47 changes: 46 additions & 1 deletion content/docs/data-modeling/validation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ All validation types share these base properties:
| `description` | `string` | optional | Developer documentation |
| `active` | `boolean` | optional | Is the rule active (default: `true`) |
| `severity` | `enum` | optional | `'error'` (blocks save), `'warning'`, `'info'` (default: `'error'`) |
| `message` | `string` | ✅ | User-facing error message |
| `message` | `string` | ✅ | The sentence a refused write returns to the person; may show the record's values in `{{ }}` holes ([Messages](#messages)) |
| `events` | `enum[]` | optional | When to run: `'insert'`, `'update'` (default: `['insert', 'update']`). No `'delete'` — guard deletions with a `beforeDelete` lifecycle hook |
| `priority` | `number` | optional | Execution order (0-9999, lower runs first, default: 100) |
| `tags` | `string[]` | optional | Categorization tags |
Expand Down Expand Up @@ -300,6 +300,51 @@ Apply validation only when a condition is met:

The outer conditional evaluates the `when` CEL predicate, then recurses into `then` (when true) or `otherwise` (when false). The nested rule supplies the violation message; the conditional itself carries the required base `message`.

## Messages

A rule's `message` is the sentence the person who saved reads when the rule refuses the write. It is a `{{ }}` template, so it can say by how much a limit was crossed, not only that it was:

```typescript
{
name: 'payment_not_above_total',
type: 'cross_field',
condition: 'record.received_amount != null && record.total_price != null && record.received_amount > record.total_price',
fields: ['received_amount', 'total_price'],
message: 'Total {{ record.total_price | currency }} is below what was received ({{ record.received_amount | currency }}).',
}
```

A refused save then reads `Total $1,200.00 is below what was received ($1,500.00).`

**What a hole may name.** One of the object's own fields, one segment deep, under one of two roots:

| Root | Holds |
| :--- | :--- |
| `record` | The record the rule judged: the stored row with this write applied |
| `previous` | The stored row before an update. On insert there is none, so a `previous` hole renders empty |

`objectstack validate`, `objectstack compile` and the publish gate refuse a single-brace token (`{total_price}`, `${total_price}`), a hole over any other root (`{{ user.name }}`), a hole that reads through a field (`{{ record.account.name }}`), a field the object does not declare, and logic in a hole. Each refusal names the spelling to use. A rule stored before these checks keeps loading and is judged at its next save.

**Formatters.** A hole takes at most one, after a `|`. Values are formatted in the language the refusal is negotiated in:

| Formatter | Renders |
| :--- | :--- |
| `number`, `number:2` | A grouped number, optionally with fixed decimals |
| `currency`, `currency:EUR` | An amount in the organization's default currency, or in the ISO 4217 code written in capitals |
| `percent`, `percent:1` | A 0–1 ratio as a percentage |
| `date`, `date:long`, `date:iso` | A calendar date |
| `datetime`, `datetime:long`, `datetime:iso` | An instant, in the organization's time zone, UTC when none is set (`iso` stays UTC) |
| `upper`, `lower`, `trim`, `truncate:80` | Text, changed or cut with an ellipsis |
| `default:'n/a'` | The text given, when the field is empty |

**Currency.** A bare `| currency` formats in the organization's default currency, set in Setup → Localization (`localization.currency`). When none is set, that hole renders empty, the rest of the sentence still fills, and the server log names the rule and the setting to fill. An amount is never shown in a currency nobody chose. A `currency` field that carries its own currency (a per-record or fixed currency) is still formatted in the organization's default by a bare `| currency`; name the code (`| currency:EUR`) or use `| number:2` for such a field.

**Fields the person cannot read stay empty.** A hole shows a value only when the person whose write was refused may read that field unmasked. A field they cannot read, a field masked for them, and a password, secret or internal field render empty, and `default:` does not fill them. The same holds when an automation writes on someone's behalf: a `runAs: 'system'` flow someone started, an action, an approval step. The sentence is worded for that person, so it shows what they may read. A system write that names no person (a schedule, a platform job) is read only in the server log, and shows every field but password, secret and internal ones. If the platform cannot tell what the person may read, every hole renders empty and the server log says why. Whether the write is refused never depends on the message.

**Translations.** A translated message at `objects.<object>._validations.<rule>.message` reads the same holes and is filled first. If it does not render, the authored message is filled instead and the server log names the translation.

**Plain text.** The filled sentence is text, never markup: a value shows exactly as stored, so a client must render it as text.

## Priority

When multiple validations exist, `priority` controls execution order:
Expand Down
16 changes: 9 additions & 7 deletions content/docs/ui/translations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,17 @@ Honest limits worth knowing before you plan around them:
position in the component tree.** That is a settled refusal (maintainer
ruling 2026-09-03, #14749) — not a gap awaiting a fix, and not something to
plan around.
- **Validation messages are translatable, but substituted whole — there is no
interpolation.** Author the message on the rule
- **Validation messages are translatable, and a translation fills the same
`{{ }}` holes.** Author the message on the rule
(`object.validations[].message`), which the engine returns on every rejected
write, and translate it under `objects.<name>._validations.<rule>.message`
(the row in the table above) — live since 17.3.0. The write path swaps the
whole sentence for the bundle's, so only the language changes: an authored
message has no parameter contract, so `{variable}` placeholders in it are not
filled, and a key the bundle does not carry falls back to the authored
literal.
(the row in the table above) — live since 17.3.0. Both are templates over
`record` and `previous`
([Messages](/docs/data-modeling/validation#messages)): the write path picks
the translation for the refusal's language and fills it first; one that does
not render falls back to the authored message, filled the same way, and the
server log names the translation. A key the bundle does not carry falls back
to the authored message. A single-brace `{variable}` is refused at publish.

The retired top-level `validationMessages` group is **not** what came back.
It was removed in 17.0.0 (#4667) because nothing read it — a translated rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const entry: SemanticMigration = {
+ '— a sentence carrying a single-brace `{…}` or `${…}` token, or a `{{ }}` hole',
replacement: 'a sentence with no braces, or a `{{ }}` hole naming one of the object\'s own fields under `record` '
+ '(the merged record the rule judged) or `previous` (the stored row before an update), one segment deep, with '
+ 'an optional whitelisted formatter: `Total {total_price} is below {received}` becomes `Total {{ '
+ 'an optional whitelisted formatter: `Total {total_price} is below {received_amount}` becomes `Total {{ '
+ 'record.total_price | currency }} is below {{ record.received_amount | currency }}`, and a brace that was '
+ 'prose (`must be { tier: standard|premium }`) is reworded without it (`must name a tier: standard or '
+ 'premium`).',
Expand Down
12 changes: 11 additions & 1 deletion scripts/check-skills-token-ratchet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,17 @@ export const CEILINGS = new Map([
// 3862 tokens, no headroom to absorb any of it, ceiling +396, the measured remainder, pinned
// AT the measurement. Raise path 「A:先削重复,余额按实测提高」 (ruling record 6513306068).
// ceil($(wc -c < skills/objectstack-data/rules/validation.md) / 4) == ceil(15448 / 4)
['skills/objectstack-data/rules/validation.md', 3862],
// 3862 -> 4097 (#37909, ruling P3 7239726323, currency ruling A 7277046538): GAINS
// § Messages — Record Values in Holes, what a refused write's sentence does since rule
// messages render (the two roots, the formatters, the tenant currency and its empty hole, the
// field floor, translation first, plain text). PAID DOWN FIRST inside the file: § Incorrect vs
// Correct and its table-of-contents entry are gone, since its one example (a rule with no
// `events` runs on insert and update; scope it with `events: ['update']`) restates § Events.
// -699 bytes cut, +1638 added: 15448 -> 16387 bytes (+939) -> 4097 tokens, no headroom to
// absorb any of it, ceiling +235, the measured remainder, pinned AT the measurement. Raise path
// 「A:先削重复,余额按实测提高」 (ruling record 6513306068).
// ceil($(wc -c < skills/objectstack-data/rules/validation.md) / 4) == ceil(16387 / 4)
['skills/objectstack-data/rules/validation.md', 4097],

// objectstack-platform
['skills/objectstack-platform/references/plugin-hooks.md', 2413], // -215 to the measurement (was 2628)
Expand Down
54 changes: 27 additions & 27 deletions skills/objectstack-data/rules/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
- [Available Rule Types](#available-rule-types) · [Expression Syntax](#expression-syntax) · [Script Validation](#script-validation)
- [Uniqueness — Use Unique Indexes](#uniqueness--use-unique-indexes) · [State Machine Validation](#state-machine-validation)
- [Format](#format-validation) · [Cross-Field](#cross-field-validation) · [JSON Schema](#json-schema-validation) · [Conditional](#conditional-validation)
- [External / Custom Validation → Lifecycle Hooks](#external--custom-validation--lifecycle-hooks) · [Validation Properties](#validation-properties)
- [Incorrect vs Correct](#incorrect-vs-correct)
- [External / Custom Validation → Lifecycle Hooks](#external--custom-validation--lifecycle-hooks) · [Messages](#messages--record-values-in-holes) · [Validation Properties](#validation-properties)

Unmarked blocks are fragments; `check:skill-examples` skips them.

Expand Down Expand Up @@ -248,6 +247,32 @@ const taxIdCheck = defineHook({
});
```

## Messages — Record Values in Holes

`message` is a `{{ }}` template: a hole names one of the object's own fields,
one segment deep, under `record` (the record the rule judged) or `previous`
(the stored row before an update; empty on insert), with at most one formatter.

```typescript
message: 'Total {{ record.total_price | currency }} is below what was received ({{ record.received_amount | currency }}).'
```

- Formatters: `number` / `number:2`, `currency` / `currency:EUR`, `percent`,
`date` / `date:long`, `datetime`, `upper`, `lower`, `trim`, `truncate:80`,
`default:'n/a'`. A single-brace `{total}`, a hole through a field
(`{{ record.account.name }}`) or over another root (`{{ user.name }}`) and
logic in a hole are refused by `os validate` and at publish.
- Bare `| currency` formats in the organization's default currency (Setup →
Localization). With none set, that hole renders empty and the server log
names the setting. A field holding its own currency names it:
`| currency:EUR`, or `| number:2`.
- A field the person whose write was refused cannot read, or sees masked,
renders empty, `default:` included. The verdict never depends on the sentence.
- The translation at `objects.<object>._validations.<rule>.message` takes the
same holes and is filled first; if it does not render, the authored message is
filled instead.
- The sentence is plain text: a value shows as stored, never as markup.

## Validation Properties

### Severity Levels
Expand Down Expand Up @@ -284,31 +309,6 @@ priority: 100 // Application validations (default)
priority: 1000 // User validations (run last)
```

## Incorrect vs Correct

### ❌ Incorrect — Validation Fires Too Often

```typescript
{
type: 'script',
condition: P`record.status == 'draft'`,
message: 'Record is still in draft',
// ❌ No events — runs on all operations
}
```

### ✅ Correct — Validation Scoped to Events

```typescript
{
name: 'no_draft_publish',
type: 'script',
condition: P`record.status == 'draft'`,
message: 'Cannot publish draft records',
events: ['update'], // ✅ Only validate on update
}
```

## Conditional Field Rules

A rule that belongs to the data model goes on the **field definition** and
Expand Down
Loading