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
10 changes: 10 additions & 0 deletions .changeset/37407-skills-multi-reason-refusal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/skills': patch
---

docs(skills): data hooks and actions teach the multi-reason refusal throw

Clause-②: no (published prose only; teaches the list throw already on main)
Metadata-protocol: none

`skills/objectstack-data/references/data-hooks.md` (Declared refusals) now teaches the list form beside the single `reason`: throw `reasons: [{ reason, params? }, …]`; each line is translated and filled from its own `params`; `userMessage` joins the lines and closes with the entry's `message`, filled from the throw's top-level `params` (else its holes render empty); the HTTP doors answer `details.refusals`. It says how an entry declares `reasons` (a snake_case reason to `{ message }` only; another status needs another code), that every listed reason must be declared (refused at save when literal, a `500` at run), and that a `reasons` that is not a non-empty list of `{ reason, params? }`, or one beside `reason`, is a `500`. `skills/objectstack-ui/rules/actions.md` (Refusing by code) shows an action's body the same list shape, `reasons: [{ reason, params? }, …]` instead of `reason`, closed by the code's `message`, and points at the hook section for the rules. Both files sat at zero token headroom, so the words are paid for by tightening prose in the same file; no fact is removed (one redundant `condition` example and a sample field go).
89 changes: 38 additions & 51 deletions skills/objectstack-data/references/data-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ data hooks.

### What Are Hooks?

Hooks are **event handlers** that execute during the ObjectQL data access lifecycle.
They intercept operations at specific points (before/after) — see [Before vs After](#before-vs-after-hooks).
Hooks are **event handlers** in the ObjectQL data access lifecycle, run before or
after an operation — see [Before vs After](#before-vs-after-hooks).

### Hook Lifecycle Events

ObjectStack provides lifecycle events organized by operation type:

| Event | When It Fires | Use Cases |
|:------|:--------------|:----------|
| **Read Operations** | | |
Expand Down Expand Up @@ -66,12 +64,11 @@ ObjectStack provides lifecycle events organized by operation type:

## Hook Definition Schema

Every hook must conform to the `HookSchema`. Author it with `defineHook()` —
preferred over a bare `: Hook` literal (the same rule as `defineDatasource`):
it validates when the module is imported, so constraint-level mistakes a bare
annotation can't catch — a non-`snake_case` `name`, a misspelled key routed
through a spread — fail while you author instead of at deploy, and the export
carries defaults already materialized.
Every hook must conform to `HookSchema`. Author it with `defineHook()`, not a
bare `: Hook` literal (the same rule as `defineDatasource`): it validates when the
module is imported, so constraint-level mistakes an annotation can't catch — a
non-`snake_case` `name`, a misspelled key routed through a spread — fail while you
author, not at deploy, and the export carries defaults already materialized.

<!-- os:check -->
```typescript
Expand Down Expand Up @@ -125,20 +122,15 @@ const myHook = defineHook({

#### `condition` — Declarative Filtering

Skip the hook if the condition is false. Author it as a **CEL
Skips the hook when false. Author it as a **CEL
predicate** over `record` and `previous` (use `P\`...\`` from
`@objectstack/spec`; SQL-style `=` / `AND` / `IN (...)` is not CEL):

```typescript
// Only run for high-value accounts
condition: P`record.annual_revenue != null && record.annual_revenue > 1000000`

// Only run for specific statuses
condition: P`record.status in ['pending', 'in_review']`

// Complex conditions
condition: P`record.type == 'enterprise' && record.region == 'APAC' && record.is_active == true`

// A TRANSITION — fires only on the update that completes the task,
// not on later updates of an already-done record
condition: P`previous.done != true && record.done == true`
Expand Down Expand Up @@ -180,8 +172,8 @@ in neither). So:
- **Guard optional values with `!= null`, never with `has(...)`.** A declared
field holding `null` is *present*, so `has(record.spent)` is uniformly true and
`has(record.spent) && record.spent > record.budget` still faults on
`null > null`. `has()` answers "is this key declared at all", which is a
question about your spelling, not about your data.
`null > null`. `has()` asks whether the key is declared at all: your
spelling, not your data.

⚠️ **An unevaluable condition ABORTS the operation.** A typo'd key
(`record.stauts`), a column read off `previous` on an insert, or a comparison
Expand Down Expand Up @@ -280,15 +272,12 @@ await ctx.api.object('task').find({ where: { $and: [{ done: false }, { owner_id:

> `filter:` is tolerated as an **object-valued** alias (normalised to `where`),
> but prefer `where`. Do **not** pass an array-of-triples such as
> `[['id', '=', x]]` — that is not a supported value shape and silently matches
> nothing.
> `[['id', '=', x]]` — an unsupported shape that silently matches nothing.

**`findOne` must say which record it wants.** It reads a single row, so an
absent or empty predicate does not come back as `null` — it comes back as the
object's **first row**: a real, plausible-looking record with nothing to do with
what you asked for, which your `if (!row)` cannot catch. So `findOne()`,
`findOne({})` and `findOne({ where: {} })` **throw**. Be specific in one
of three ways:
**`findOne` must say which record it wants.** An absent or empty
predicate would return not `null` but the object's **first row**, an unrelated
record your `if (!row)` cannot catch. So `findOne()`, `findOne({})`
and `findOne({ where: {} })` **throw**. Be specific in one of three ways:

```js
await ctx.api.object('candidate').findOne({ where: { id } }); // by predicate
Expand All @@ -298,15 +287,14 @@ await ctx.api.object('audit')
await ctx.api.object('candidate').find({ limit: 1 }); // any row will do
```

An unpredicated `find` / `count` is fine — returning or counting every row is an
honest answer.
An unpredicated `find` / `count` is fine: every row is an honest answer.

### Capabilities — the complete list

A body may only touch a `ctx` API it declared in `capabilities`. Calling an
undeclared one **throws inside the VM** — `capability '<token>' not granted to
hook '<name>' …` — which surfaces as a hook error (see Troubleshooting). The full
set of legal tokens (`HookBodyCapability`) is exactly seven:
hook '<name>' …` — which surfaces as a hook error (see Troubleshooting). The legal
tokens (`HookBodyCapability`) are exactly seven:

| Token | Unlocks |
|:--|:--|
Expand Down Expand Up @@ -424,17 +412,14 @@ export default fillPositionOnHire;
`log` capability the call **throws** (surfaces as a hook error); (2) **with** the
capability it emits only when the runtime wired a logger into the hook context —
otherwise it is a **silent no-op**. Treat `ctx.log` as best-effort diagnostics,
not a reliable side-channel or proof a hook ran; to observe an effect, assert on
the data it writes.
never proof a hook ran; to observe an effect, assert on the data it writes.

---

## Hook Context API

> The `ctx` below is the deprecated in-process `handler`'s; a `body` sees the
> [sandbox subset](#what-lives-on-the-sandbox-ctx).

The engine's `HookContext` provides:
> The `ctx` below, the engine's `HookContext`, is the deprecated in-process
> `handler`'s; a `body` sees the [sandbox subset](#what-lives-on-the-sandbox-ctx).

### Context Properties

Expand Down Expand Up @@ -504,7 +489,7 @@ const org = ctx.user?.organizationId ?? ctx.session?.organizationId;

### `input` — Operation Parameters

The structure of `ctx.input` varies by event:
`ctx.input` varies by event:

**Insert operations:**
```typescript
Expand All @@ -513,7 +498,6 @@ The structure of `ctx.input` varies by event:
// All field values being inserted
name: 'Acme Corp',
industry: 'Technology',
annual_revenue: 5000000,
...
}
```
Expand Down Expand Up @@ -770,7 +754,6 @@ ctx.ql.registerHook('beforeInsert', async (hookCtx) => { /* ... */ },
file, default-exported, then listed in the stack's `hooks` array (method 1). This is
the layout both real hook modules in the repo use.


---

## Best Practices
Expand All @@ -791,7 +774,6 @@ hook (its `ctx.api` writes roll back with the write) or record it in the caller,
where the write is made; assume `ctx.session` exists — system operations carry
no user.


---

## Error Handling
Expand All @@ -802,10 +784,9 @@ A throw aborts the operation and rolls back its transaction; `onError: 'log'`
### Declared refusals (`refusals`)

A hook that enforces a business rule declares a `refusals` catalogue, one
SCREAMING_SNAKE code per rule, and the hook throws the code. Without one, a `body`'s
throw is answered `HOOK_ABORTED`, the platform's code: fine for a failure you did not
plan, not for a rule the user can act on. A cross-object guard a validation rule
cannot read:
SCREAMING_SNAKE code per rule, and throws the code. Without one, a `body`'s
throw is answered `HOOK_ABORTED`, the platform's code: fine for an unplanned failure, not
for a rule the user can act on. A cross-object guard a validation rule cannot read:

<!-- os:check -->
```typescript
Expand Down Expand Up @@ -840,15 +821,21 @@ export default defineHook({
- A `js` `body` or a `handler` (inline or named) can declare one; an
`expression` body, or neither, is refused at save.
- The wire `code` is the status's (default 400), with the hook's as `declaredCode`.
- `reasons` (snake_case, thrown as `reason`) add sentences that keep the code's wire
`code` and status.
- `params` names what a `{{ name }}` hole may fill (thrown as `params`); a hole
over any other name, or any hole with no `params`, is refused.
- `reasons` maps a snake_case reason (thrown as `reason`) to `{ message }` only: a
sentence under the code's wire `code` and status; another status needs another code.
- `params` names what a `{{ name }}` hole, a reason's too, may fill (thrown as
`params`); a hole over any other name, or any hole with no `params`, is refused.
- For several reasons throw `reasons: [{ reason: 'on_loan', params: { due } }, …]`, not
`reason`: one translated line each, filled from its own `params`. `userMessage` joins
them by newlines and ends with the entry's `message`, filled from the throw's
top-level `params` (else its holes are empty); the HTTP doors answer
`details.refusals: [{ reason, message }]`, absent for a single `reason`.
- Translated via `hooks.<hook>.refusals.<CODE>.message` (or
`….<CODE>.reasons.<REASON>.message`); the thrown text and `status` are unused.
- An undeclared code (or none), or a reason missing from a code's `reasons`, is
refused: at save if thrown literally, else as a `500`. Unchanged `ctx.api`
errors and crashes pass through.
- An undeclared code (or none), or a reason missing from a code's `reasons` (a listed
one even when the code declares none), is refused: at save if thrown literally, else
as a `500`. A `reasons` not a non-empty list of `{ reason, params? }`, or beside
`reason`, is a `500`. Unchanged `ctx.api` errors and crashes pass through.

### Declared texts (`texts`)

Expand Down
Loading
Loading