Skip to content

Commit fa8fc75

Browse files
committed
fix(lint): readonly write rules judge an extension field's served definition; explain texts count extension fields as declared
Claude-Session: https://claude.ai/code/session_01WGufzft7TrxiryiTZm4o9p Co-authored-by: Claude <noreply@anthropic.com>
1 parent 482ad5c commit fa8fc75

6 files changed

Lines changed: 115 additions & 11 deletions

File tree

‎content/docs/automation/hook-bodies.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Static validation around a hook is asymmetric, and it is worth knowing exactly w
217217

218218
- **Checked — read side.** `hook.condition` is validated at build time against the target object's fields by the expression validator (`@objectstack/lint`), including array-valued `hook.object` targets. A condition referencing a nonexistent field fails the lint.
219219
- **Checked — capability side.** `body.capabilities` gates which `ctx` APIs the body may call at all; the sandbox throws on an undeclared call.
220-
- **Checked — write side, literal-only.** Since [#4271](https://github.com/objectstack-ai/objectstack/issues/4271), `body.source` is **parsed** (never executed, never type-checked) and the field names it writes are resolved against the target object's declarations. An unknown field raises `hook-body-write-unknown-field` — a **warning** carrying a did-you-mean suggestion, which never blocks a build. Action bodies get the same check on their `ctx.api` writes (`action-body-write-unknown-field`). Both run under `os validate`, `os lint` and `os compile`. The *existence* question is advisory like this because the answer can depend on a package the build cannot see; the separate *writability* question — [writing a `readonly` field through `ctx.api`](#writing-a-readonly-field) — **does** gate, because both halves of that judgement are declared in the stack being checked.
220+
- **Checked — write side, literal-only.** Since [#4271](https://github.com/objectstack-ai/objectstack/issues/4271), `body.source` is **parsed** (never executed, never type-checked) and the field names it writes are resolved against the target object's declarations, which include every field an `objectExtensions` entry of the same artifact adds to it. An unknown field raises `hook-body-write-unknown-field` — a **warning** carrying a did-you-mean suggestion, which never blocks a build. Action bodies get the same check on their `ctx.api` writes (`action-body-write-unknown-field`). Both run under `os validate`, `os lint` and `os compile`. The *existence* question is advisory like this because the answer can depend on a package the build cannot see; the separate *writability* question — [writing a `readonly` field through `ctx.api`](#writing-a-readonly-field) — **does** gate, because both halves of that judgement are declared in the stack being checked.
221221
- **Checked — writes to a system column the object has no storage for.** Since [#8663](https://github.com/objectstack-ai/objectstack/issues/8663), a write to an injected system column is no longer exempted on the strength of its NAME alone. The registry injects `owner_id` / `organization_id` / the audit family onto an ADR-0015 [`external` object](/docs/data-modeling/external-datasources) exactly as onto a local one, but the remote database owns that schema and no column exists behind them. Writing one raises `hook-body-write-unprovisioned-anchor` (or `action-body-write-unprovisioned-anchor` / `flow-node-write-unprovisioned-anchor` on the other two surfaces) — a **warning** on all three, including the flow-node rule that otherwise gates, because the claim is about a remote schema the build cannot see. A column you **declare** yourself is untouched: on a federated object a declared `owner_id` maps a remote column you vouch for. Why it matters more than an ordinary typo: an undeclared name is refused upstream by the engine's own write-path validator (`INVALID_FIELD`), whereas the injected anchor is in the registered schema and passes it — so it is the one payload key that reaches the remote database raw, where a SQL remote aborts the **whole statement** with an untyped `no such column` and takes the correctly named fields of the same payload with it.
222222
- **Checked — writes that reach nothing at all.** Since [#4345](https://github.com/objectstack-ai/objectstack/issues/4345), an action body assigning to `ctx.record` raises `action-record-write-discarded`, also a warning. This one is **not** a field-resolution question: an action's `ctx.record` is a snapshot the runtime never writes back, so the assignment is discarded whether or not the field is declared — see [Signature conventions](#signature-conventions) below.
223223

‎packages/lint/src/object-graph.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import { ACTION_BODY_WRITE_UNKNOWN_FIELD, validateActionBodyWrites } from './val
4040
import { HOOK_BODY_WRITE_UNKNOWN_FIELD, validateHookBodyWrites } from './validate-hook-body-writes.js';
4141
import { validateReactPageProps } from './validate-react-page-props.js';
4242
import { TRANSLATION_TARGET_UNKNOWN, validateTranslationReferences } from './validate-translation-references.js';
43+
import { FLOW_UPDATE_READONLY_FIELD, validateReadonlyFlowWrites } from './validate-readonly-flow-writes.js';
44+
import { HOOK_API_UPDATE_READONLY_FIELD, validateReadonlyHookWrites } from './validate-readonly-hook-writes.js';
45+
import { ACTION_API_UPDATE_READONLY_WHEN_FIELD, validateReadonlyActionWrites } from './validate-readonly-action-writes.js';
46+
import { SEARCHABLE_FIELD_UNKNOWN } from './validate-searchable-fields.js';
4347
// [#16340] Read back the registry's OWN definition table to assert the graph
4448
// derives its injected types rather than carrying a second copy of them.
4549
import { injectedSystemColumnDefs } from '@objectstack/spec/data';
@@ -946,4 +950,85 @@ describe('[#30471] the field-name indexes read the objects as served', () => {
946950
expect(messages(text, {})).toEqual([expect.stringContaining('type mismatch'), expect.stringContaining('nullable')]);
947951
expect(messages(num, {})).toEqual([expect.stringContaining('nullable')]);
948952
});
953+
954+
it('validate-expressions, on the per-package leg: a tied field is judged on no definition either', () => {
955+
const body = { id: 'app', objects: [{ ...deal, validations: [{ name: 'v', type: 'script', condition: 'record.score + 1 > 2', message: 'M' }] }, account] };
956+
const tie = (a: Rec, b: Rec): Rec => ({
957+
...body,
958+
manifest: body,
959+
packages: [
960+
{ manifest: body },
961+
{ manifest: { id: 'mod', objectExtensions: [{ extend: 'xs_deal', fields: a }] } },
962+
{ manifest: { id: 'mod2', objectExtensions: [{ extend: 'xs_deal', fields: b }] } },
963+
],
964+
});
965+
const messages = (a: Rec, b: Rec) => validateStackExpressions(tie(a, b)).map((issue) => issue.message);
966+
const num = { score: { type: 'number' } };
967+
const text = { score: { type: 'text' } };
968+
expect(messages(num, text)).toEqual([]);
969+
expect(messages(text, num)).toEqual([]);
970+
expect(messages(text, {})).toEqual([expect.stringContaining('type mismatch'), expect.stringContaining('nullable')]);
971+
});
972+
973+
// The readonly write rules judge an extension field's `readonly` /
974+
// `readonlyWhen` as a declared field's: without that, moving the existence
975+
// index above would have left such a write silent where `main` at least
976+
// refused the name. Readonly is the field's definition, so a tie is unjudged.
977+
describe('the readonly write rules read the served definition', () => {
978+
const lock = { locked: { type: 'text', readonly: true } };
979+
const gate = { gated: { type: 'number', readonlyWhen: "record.name == 'x'" } };
980+
// rule · its readonly id · the extension fields it needs · the stack writing them
981+
const readonly: Array<[string, string, Run, Rec, Rec]> = [
982+
['validate-readonly-flow-writes', FLOW_UPDATE_READONLY_FIELD, validateReadonlyFlowWrites, lock, {
983+
flows: [{ name: 'fl', nodes: [{ id: 'n', type: 'update_record', config: { objectName: 'xs_deal', fields: { locked: 'x' } } }] }],
984+
}],
985+
['validate-readonly-hook-writes', HOOK_API_UPDATE_READONLY_FIELD, (stack) => validateReadonlyHookWrites(stack), lock, {
986+
hooks: [{ name: 'h', object: 'xs_account', events: ['afterInsert'], body: { language: 'js', source: "await ctx.api.object('xs_deal').update({ id: dealId, locked: 'x' });" } }],
987+
}],
988+
['validate-readonly-action-writes', ACTION_API_UPDATE_READONLY_WHEN_FIELD, validateReadonlyActionWrites, gate, {
989+
actions: [{ name: 'act', label: 'Act', objectName: 'xs_deal', body: { language: 'js', source: "await ctx.api.object('xs_deal').update({ id: ctx.recordId, gated: 1 });" } }],
990+
}],
991+
];
992+
const plain = { locked: { type: 'text' }, gated: { type: 'number' } };
993+
const ext = (fields: Rec) => [{ extend: 'xs_deal', fields }];
994+
const tie = (a: Rec, b: Rec, extra: Rec, perPackageLeg = false): Rec => {
995+
const mods = [
996+
{ manifest: { id: 'mod', objectExtensions: ext(a) } },
997+
{ manifest: { id: 'mod2', objectExtensions: ext(b) } },
998+
];
999+
if (!perPackageLeg) return { objects: [deal, account], packages: [{ manifest: { id: 'app', objects: [deal, account] } }, ...mods], ...extra };
1000+
const body = { id: 'app', objects: [deal, account], ...extra };
1001+
return { ...body, manifest: body, packages: [{ manifest: body }, ...mods] };
1002+
};
1003+
1004+
it.each(readonly)('%s: an extension field declared readonly is refused with the readonly id', (_rule, id, run, fields, writing) => {
1005+
expect(ids(run, artifact(writing, ext(fields)))).toEqual([id]);
1006+
expect(ids(run, perPackage(writing, ext(fields)))).toEqual([id]);
1007+
// The same name added writable, or not added at all, is not this rule's.
1008+
expect(ids(run, artifact(writing, ext(plain)))).toEqual([]);
1009+
expect(ids(run, artifact(writing, []))).toEqual([]);
1010+
});
1011+
1012+
it.each(readonly)('%s: a field two packages tie on, one of them readonly, is unjudged', (_rule, id, run, fields, writing) => {
1013+
for (const perPackageLeg of [false, true]) {
1014+
expect(ids(run, tie(plain, fields, writing, perPackageLeg))).toEqual([]);
1015+
expect(ids(run, tie(fields, plain, writing, perPackageLeg))).toEqual([]);
1016+
expect(ids(run, tie(fields, {}, writing, perPackageLeg))).toEqual([id]);
1017+
}
1018+
});
1019+
});
1020+
1021+
// `indexObjectSearchTargets` stays on the declared objects (its type
1022+
// question is the searchable-fields rule's, not moved here), so a react
1023+
// page's `searchableFields` naming an extension field is still refused.
1024+
it('validate-react-page-props: searchableFields still reads the declared objects', () => {
1025+
const search = (field: string) => ({
1026+
pages: [{
1027+
name: 'p', kind: 'react',
1028+
source: `function Page(){ return <ListView data={{ provider: 'object', object: 'xs_deal' }} columns={['name']} searchableFields={['name', '${field}']} />; }`,
1029+
}],
1030+
});
1031+
expect(ids(validateReactPageProps, artifact(search('region')))).toEqual([SEARCHABLE_FIELD_UNKNOWN]);
1032+
expect(ids(validateReactPageProps, artifact(search('name')))).toEqual([]);
1033+
});
9491034
});

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,8 @@ const DECLARED_FIELD_DOOR =
10911091
'every driver, before any statement is built. The whole payload is refused, so the correctly named ' +
10921092
'fields beside the bad key never land either. The refusal arrives at run time, on whichever record ' +
10931093
'first exercises the write and far from the line that made the mistake, which is why the rule ' +
1094-
'reports it at author time, naming the field, the object and the write.';
1094+
'reports it at author time, naming the field, the object and the write. Declared includes every field ' +
1095+
'an `objectExtensions` entry of the same artifact adds to the object, which the registry serves.';
10951096

10961097
/** The cause and the consequence of writing an unprovisioned anchor — the three anchor ids. */
10971098
const UNPROVISIONED_ANCHOR_WRITE: readonly string[] = [
@@ -3898,8 +3899,8 @@ const TRANSLATION_TARGET_UNKNOWN_EXPLANATION: RuleExplanation = {
38983899
'The verdict names the nearest declared name when one is close ("Did you mean"), and the fix ' +
38993900
'line names the remedy only, without listing what is declared; what each key may name follows.',
39003901
'Under `objects.OBJECT` (an object this stack, or a sibling package of the same artifact, ' +
3901-
'defines): `fields.FIELD` a declared field (a system column, `_id`, `name` and `space` are ' +
3902-
'never reported); `_views.VIEW` a view\'s `name`, not its label — the runtime keys a nameless ' +
3902+
'defines): `fields.FIELD` a declared field, fields an `objectExtensions` entry of the same ' +
3903+
'artifact adds included (a system column, `_id`, `name` and `space` are never reported); `_views.VIEW` a view\'s `name`, not its label — the runtime keys a nameless ' +
39033904
'default `list` `default`, never `list`; `_sections.KEY` a ' +
39043905
'`fieldGroups[].key`, a named form-view section or a named `record:details` section — a ' +
39053906
'section is translatable only through a stable name; `_tabs.TAB` a filter-preset tab that a ' +
@@ -4327,7 +4328,8 @@ const PAGE_FIELD_UNKNOWN_EXPLANATION: RuleExplanation = {
43274328
covers: 'how a page binding names a field',
43284329
paragraphs: [
43294330
'A page component binds fields by name, and each name must match one of the bound object\'s ' +
4330-
'declared fields, or a system field, exactly: a misspelt or renamed field resolves to nothing, ' +
4331+
'declared fields (declared includes fields an `objectExtensions` entry of the same artifact ' +
4332+
'adds), or a system field, exactly: a misspelt or renamed field resolves to nothing, ' +
43314333
'at the cost the message names.',
43324334
'To fix it, correct the name (the `fix:` line names the nearest declared field when one is ' +
43334335
'close) or add the field to the object. Not judged here: a dotted relationship path ' +

‎packages/lint/src/validate-readonly-action-writes.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ import {
109109
type BodyWritePatternExclusion,
110110
} from './validate-hook-body-writes.js';
111111
import { buildReadonlyIndex, READONLY_WHEN_STRIP_SCOPE } from './validate-readonly-flow-writes.js';
112-
import { recordsOf } from './object-graph.js';
112+
import { objectsAsServed, objectsWithoutTiedFields } from './object-graph.js';
113113

114114
export type ReadonlyActionWriteSeverity = 'warning';
115115

@@ -264,7 +264,12 @@ export function validateReadonlyActionWrites(stack: AnyRec): ReadonlyActionWrite
264264
);
265265
if (writes.length === 0) continue;
266266

267-
roIndex ??= buildReadonlyIndex(recordsOf(stack.objects));
267+
// [#30471] The readonly index reads the objects as SERVED (`objectsAsServed`,
268+
// the package's one fold): a write to a field a sibling package adds
269+
// through `objectExtensions` and declares `readonly` is judged as a declared
270+
// one is. Readonly is a question about the field's definition, so a field two
271+
// packages tie on is left out (`objectsWithoutTiedFields`) and goes unjudged.
272+
roIndex ??= buildReadonlyIndex(objectsWithoutTiedFields(objectsAsServed(stack)));
268273

269274
const where = `action "${site.name}" > body`;
270275
const reported = new Set<string>();

‎packages/lint/src/validate-readonly-flow-writes.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
// flows are held to the same bar.
6565

6666
import { walkFlowNodes, flowNodeLabel } from './flow-walk.js';
67-
import { recordsOf } from './object-graph.js';
67+
import { objectsAsServed, objectsWithoutTiedFields, recordsOf } from './object-graph.js';
6868

6969
export type ReadonlyFlowWriteSeverity = 'error' | 'warning';
7070

@@ -245,7 +245,13 @@ export function validateReadonlyFlowWrites(stack: AnyRec): ReadonlyFlowWriteFind
245245
if (flows.length === 0) return findings;
246246

247247
const objects = recordsOf(stack.objects);
248-
const roIndex = buildReadonlyIndex(objects);
248+
// [#30471] The readonly index reads the objects as SERVED (`objectsAsServed`,
249+
// the package's one fold): a write to a field a sibling package adds
250+
// through `objectExtensions` and declares `readonly` is judged as a declared
251+
// one is. Readonly is a question about the field's definition, so a field two
252+
// packages tie on is left out (`objectsWithoutTiedFields`) and goes unjudged.
253+
// The insert-strip exemption is object-level and stays on the declared objects.
254+
const roIndex = buildReadonlyIndex(objectsWithoutTiedFields(objectsAsServed(stack)));
249255
const insertStripExempt = buildInsertStripExemptObjects(objects);
250256

251257
flows.forEach((flow, flowIndex) => {

‎packages/lint/src/validate-readonly-hook-writes.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ import {
178178
READONLY_INSERT_STRIP_OUTCOME,
179179
READONLY_WHEN_STRIP_SCOPE,
180180
} from './validate-readonly-flow-writes.js';
181-
import { recordsOf } from './object-graph.js';
181+
import { objectsAsServed, objectsWithoutTiedFields, recordsOf } from './object-graph.js';
182182

183183
export type ReadonlyHookWriteSeverity = 'error' | 'warning';
184184

@@ -404,7 +404,13 @@ export function validateReadonlyHookWrites(
404404
);
405405
if (writes.length === 0) return;
406406

407-
roIndex ??= buildReadonlyIndex(recordsOf(stack.objects));
407+
// [#30471] The readonly index reads the objects as SERVED (`objectsAsServed`,
408+
// the package's one fold): a write to a field a sibling package adds
409+
// through `objectExtensions` and declares `readonly` is judged as a declared
410+
// one is. Readonly is a question about the field's definition, so a field two
411+
// packages tie on is left out (`objectsWithoutTiedFields`) and goes unjudged.
412+
// The insert-strip exemption is object-level and stays on the declared objects.
413+
roIndex ??= buildReadonlyIndex(objectsWithoutTiedFields(objectsAsServed(stack)));
408414
insertStripExempt ??= buildInsertStripExemptObjects(recordsOf(stack.objects));
409415

410416
const hookName = typeof hook.name === 'string' && hook.name ? hook.name : `#${hookIndex}`;

0 commit comments

Comments
 (0)