Skip to content

Commit 105c986

Browse files
committed
fix(lint): sort indexes read the objects as served; readonly rules keep the declares-nothing skip
validate-sortable-fields builds its existence index over objectsAsServed and its sortability projection over objectsWithoutTiedFields, so a page or list-view sort naming an extension-added field passes, and one naming an extension formula field is refused as unsortable rather than unknown. An object that declares no field map keeps its declared answer on both. The three readonly write rules share buildServedReadonlyIndex, which keeps an object that declares no field unjudged, extended or not, as the write rules leave it: before this, an extension adding a readonly field to such an object made the readonly rules refuse a write main left silent. Pins: two sortable rows in the existence table (both legs, byte-equal), and a table of every refusal this change adds (rule, severity, both legs, the declared-only control), with tie and declares-nothing legs. Claude-Session: https://claude.ai/code/session_01L6bhgTqqeHSwZYJmApbDCa Co-authored-by: Claude <noreply@anthropic.com>
1 parent cc67f35 commit 105c986

5 files changed

Lines changed: 168 additions & 30 deletions

File tree

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

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ 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';
43+
import { FLOW_UPDATE_READONLY_FIELD, FLOW_UPDATE_READONLY_WHEN_FIELD, validateReadonlyFlowWrites } from './validate-readonly-flow-writes.js';
44+
import { HOOK_API_UPDATE_READONLY_FIELD, HOOK_API_UPDATE_READONLY_WHEN_FIELD, validateReadonlyHookWrites } from './validate-readonly-hook-writes.js';
45+
import { SORT_FIELD_UNKNOWN, SORT_FIELD_UNSORTABLE, validateSortableFields } from './validate-sortable-fields.js';
4546
import { ACTION_API_UPDATE_READONLY_WHEN_FIELD, validateReadonlyActionWrites } from './validate-readonly-action-writes.js';
4647
import { SEARCHABLE_FIELD_UNKNOWN } from './validate-searchable-fields.js';
4748
// [#16340] Read back the registry's OWN definition table to assert the graph
@@ -853,6 +854,12 @@ describe('[#30471] the field-name indexes read the objects as served', () => {
853854
const validationOn = (condition: string): Rec => ({
854855
objects: [{ ...deal, validations: [{ name: 'v', type: 'script', condition, message: 'M' }] }, account],
855856
});
857+
const pageSort = (field: string): Rec => ({
858+
pages: [{ name: 'p', type: 'list', object: 'xs_deal', interfaceConfig: { source: 'xs_deal', sort: [{ field, order: 'asc' }] } }],
859+
});
860+
const viewSort = (field: string): Rec => ({
861+
views: [{ name: 'v', objectName: 'xs_deal', list: { type: 'grid', columns: ['name'], sort: [{ field, order: 'asc' }] } }],
862+
});
856863

857864
// rule · its unknown-field id · the stack naming FIELD
858865
const existence: Array<[string, string, Run, (field: string) => Rec]> = [
@@ -878,6 +885,8 @@ describe('[#30471] the field-name indexes read the objects as served', () => {
878885
['validate-translation-references', TRANSLATION_TARGET_UNKNOWN, validateTranslationReferences, (f) => ({
879886
translations: [{ 'zh-CN': { objects: { xs_deal: { fields: { [f]: { label: 'L' } } } } } }],
880887
})],
888+
['validate-sortable-fields (a page sort)', PAGE_FIELD_UNKNOWN, validateSortableFields, (f) => pageSort(f)],
889+
['validate-sortable-fields (a list-view sort)', SORT_FIELD_UNKNOWN, validateSortableFields, (f) => viewSort(f)],
881890
];
882891

883892
it.each(existence)('%s: an extension-added field passes; a field nothing adds is still refused', (_rule, id, run, naming) => {
@@ -1031,4 +1040,89 @@ describe('[#30471] the field-name indexes read the objects as served', () => {
10311040
expect(ids(validateReactPageProps, artifact(search('region')))).toEqual([SEARCHABLE_FIELD_UNKNOWN]);
10321041
expect(ids(validateReactPageProps, artifact(search('name')))).toEqual([]);
10331042
});
1043+
1044+
// Every refusal this change adds, one row each: a write or a sort naming an
1045+
// extension field whose served definition the runtime refuses or drops. On
1046+
// the declared objects alone (what `main` read) the same stack was the
1047+
// existence rule's unknown name; now the existence rule passes it and the
1048+
// definition rule owns it, at that rule's own severity. The hook rows are
1049+
// the one severity move: `hook-body-write-unknown-field` was a warning.
1050+
describe('each refusal this change adds is the definition rule\'s, at its own severity', () => {
1051+
const lock = { locked: { type: 'text', readonly: true } };
1052+
const gate = { gated: { type: 'number', readonlyWhen: "record.name == 'x'" } };
1053+
const calc = { calc: { type: 'formula', expression: 'record.amount * 2' } };
1054+
const flowNode = (type: string, field: string): Rec => ({
1055+
flows: [{ name: 'fl', nodes: [{ id: 'n', type, config: { objectName: 'xs_deal', fields: { [field]: 'x' } } }] }],
1056+
});
1057+
const hookApi = (call: string): Rec => ({
1058+
hooks: [{ name: 'h', object: 'xs_account', events: ['afterInsert'], body: { language: 'js', source: `await ctx.api.object('xs_deal').${call};` } }],
1059+
});
1060+
const actionApi = (field: string): Rec => ({
1061+
actions: [{ name: 'act', label: 'Act', objectName: 'xs_deal', body: { language: 'js', source: `await ctx.api.object('xs_deal').update({ id: ctx.recordId, ${field}: 1 });` } }],
1062+
});
1063+
const hooks: Run = (stack) => validateReadonlyHookWrites(stack);
1064+
const hookExistence: Run = (stack) => validateHookBodyWrites(stack);
1065+
// the write · the rule judging it now · the extension fields · the stack ·
1066+
// its id and severity now · the existence rule and the id it raised before
1067+
const refusals: Array<[string, Run, Rec, Rec, string, string, Run, string]> = [
1068+
['a flow update_record of a readonly field', validateReadonlyFlowWrites, lock, flowNode('update_record', 'locked'),
1069+
FLOW_UPDATE_READONLY_FIELD, 'error', validateFlowNodeWrites, FLOW_NODE_WRITE_UNKNOWN_FIELD],
1070+
['a flow create_record of a readonly field', validateReadonlyFlowWrites, lock, flowNode('create_record', 'locked'),
1071+
FLOW_UPDATE_READONLY_FIELD, 'error', validateFlowNodeWrites, FLOW_NODE_WRITE_UNKNOWN_FIELD],
1072+
['a flow update_record of a readonlyWhen field', validateReadonlyFlowWrites, gate, flowNode('update_record', 'gated'),
1073+
FLOW_UPDATE_READONLY_WHEN_FIELD, 'warning', validateFlowNodeWrites, FLOW_NODE_WRITE_UNKNOWN_FIELD],
1074+
['a hook ctx.api update of a readonly field', hooks, lock, hookApi("update({ id: dealId, locked: 'x' })"),
1075+
HOOK_API_UPDATE_READONLY_FIELD, 'error', hookExistence, HOOK_BODY_WRITE_UNKNOWN_FIELD],
1076+
['a hook ctx.api insert of a readonly field', hooks, lock, hookApi("insert({ locked: 'x' })"),
1077+
HOOK_API_UPDATE_READONLY_FIELD, 'error', hookExistence, HOOK_BODY_WRITE_UNKNOWN_FIELD],
1078+
['a hook ctx.api update of a readonlyWhen field', hooks, gate, hookApi('update({ id: dealId, gated: 1 })'),
1079+
HOOK_API_UPDATE_READONLY_WHEN_FIELD, 'warning', hookExistence, HOOK_BODY_WRITE_UNKNOWN_FIELD],
1080+
['an action ctx.api update of a readonlyWhen field', validateReadonlyActionWrites, gate, actionApi('gated'),
1081+
ACTION_API_UPDATE_READONLY_WHEN_FIELD, 'warning', validateActionBodyWrites, ACTION_BODY_WRITE_UNKNOWN_FIELD],
1082+
['a page sort by a formula field', validateSortableFields, calc, pageSort('calc'),
1083+
SORT_FIELD_UNSORTABLE, 'error', validateSortableFields, PAGE_FIELD_UNKNOWN],
1084+
['a list-view sort by a formula field', validateSortableFields, calc, viewSort('calc'),
1085+
SORT_FIELD_UNSORTABLE, 'error', validateSortableFields, SORT_FIELD_UNKNOWN],
1086+
];
1087+
// The stack on both CLI legs (the union artifact, then the per-package
1088+
// body), each extension list its own package.
1089+
const onLegs = (extra: Rec, packages: unknown[][], objects: unknown[] = [deal, account]): Rec[] => {
1090+
const mods = packages.map((objectExtensions, i) => ({ manifest: { id: `mod${i}`, objectExtensions } }));
1091+
const body = { id: 'app', objects, ...extra };
1092+
return [
1093+
{ objects, packages: [{ manifest: { id: 'app', objects } }, ...mods], ...extra },
1094+
{ ...body, manifest: body, packages: [{ manifest: body }, ...mods] },
1095+
];
1096+
};
1097+
const ext = (fields: Rec) => [{ extend: 'xs_deal', fields }];
1098+
1099+
it.each(refusals)('%s', (_write, run, fields, stack, id, severity, existence, unknownId) => {
1100+
for (const served of onLegs(stack, [ext(fields)])) {
1101+
expect(run(served).map((f) => [f.rule, f.severity])).toEqual([[id, severity]]);
1102+
expect(ids(existence, served)).not.toContain(unknownId);
1103+
}
1104+
for (const declared of onLegs(stack, [[]])) {
1105+
expect(ids(existence, declared)).toContain(unknownId);
1106+
expect(ids(run, declared)).not.toContain(id);
1107+
}
1108+
});
1109+
1110+
it.each(refusals)('%s, on a field two packages tie on: unjudged', (_write, run, fields, stack, id) => {
1111+
const plain = Object.fromEntries(Object.keys(fields).map((name) => [name, { type: 'text' }]));
1112+
for (const [a, b] of [[plain, fields], [fields, plain]]) {
1113+
for (const tied of onLegs(stack, [ext(a), ext(b)])) expect(ids(run, tied)).not.toContain(id);
1114+
}
1115+
for (const single of onLegs(stack, [ext(fields), ext({})])) expect(ids(run, single)).toContain(id);
1116+
});
1117+
1118+
// An object that declares no field (an introspected schema) is left
1119+
// unjudged by the write rules and skip 2 of the sort rule; an extension
1120+
// adding to it does not make it judgeable by the definition rules either.
1121+
it.each(refusals)('%s, on an object that declares no field: unjudged, extended or not', (_write, run, fields, stack) => {
1122+
const bare = { name: 'xs_deal', label: 'Deal', fields: {} };
1123+
for (const packages of [[ext(fields)], [[]]]) {
1124+
for (const s of onLegs(stack, packages, [bare, account])) expect(ids(run, s)).toEqual([]);
1125+
}
1126+
});
1127+
});
10341128
});

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ import {
108108
extractHookBodyWriteSet,
109109
type BodyWritePatternExclusion,
110110
} from './validate-hook-body-writes.js';
111-
import { buildReadonlyIndex, READONLY_WHEN_STRIP_SCOPE } from './validate-readonly-flow-writes.js';
112-
import { objectsAsServed, objectsWithoutTiedFields } from './object-graph.js';
111+
import { buildReadonlyIndex, buildServedReadonlyIndex, READONLY_WHEN_STRIP_SCOPE } from './validate-readonly-flow-writes.js';
113112

114113
export type ReadonlyActionWriteSeverity = 'warning';
115114

@@ -264,12 +263,9 @@ export function validateReadonlyActionWrites(stack: AnyRec): ReadonlyActionWrite
264263
);
265264
if (writes.length === 0) continue;
266265

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)));
266+
// [#30471] The readonly index reads the objects as served
267+
// (`buildServedReadonlyIndex`).
268+
roIndex ??= buildServedReadonlyIndex(stack);
273269

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

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ export function buildReadonlyIndex(objects: AnyRec[]): Map<string, Map<string, F
162162
return idx;
163163
}
164164

165+
/**
166+
* [#30471] {@link buildReadonlyIndex} over the objects as SERVED
167+
* (`objectsAsServed`, the package's one fold): a write to a field a sibling
168+
* package adds through `objectExtensions` and declares `readonly` or
169+
* `readonlyWhen` is judged as a declared one is. Readonly is a question about
170+
* the field's definition, so a field two packages tie on is left out
171+
* (`objectsWithoutTiedFields`) and goes unjudged. An object that declares no
172+
* field keeps its own empty map, extended or not: the write rules leave such an
173+
* object (an introspected schema) unjudged, and the readonly rules do too.
174+
* Shared by the flow, hook and action readonly rules.
175+
*/
176+
export function buildServedReadonlyIndex(stack: AnyRec): Map<string, Map<string, FieldReadonlyMeta>> {
177+
const idx = buildReadonlyIndex(objectsWithoutTiedFields(objectsAsServed(stack)));
178+
for (const [name, own] of buildReadonlyIndex(recordsOf(stack.objects))) {
179+
if (own.size === 0) idx.set(name, own);
180+
}
181+
return idx;
182+
}
183+
165184
/**
166185
* The `managedBy` buckets the engine's create-side strip steps around, mirroring
167186
* `PLATFORM_INTERNAL_MANAGED_BUCKETS` in
@@ -245,13 +264,10 @@ export function validateReadonlyFlowWrites(stack: AnyRec): ReadonlyFlowWriteFind
245264
if (flows.length === 0) return findings;
246265

247266
const objects = recordsOf(stack.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)));
267+
// [#30471] The readonly index reads the objects as served
268+
// (`buildServedReadonlyIndex`). The insert-strip exemption is object-level
269+
// and stays on the declared objects.
270+
const roIndex = buildServedReadonlyIndex(stack);
255271
const insertStripExempt = buildInsertStripExemptObjects(objects);
256272

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

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ import {
174174
} from './validate-hook-body-writes.js';
175175
import {
176176
buildReadonlyIndex,
177+
buildServedReadonlyIndex,
177178
buildInsertStripExemptObjects,
178179
READONLY_INSERT_STRIP_OUTCOME,
179180
READONLY_WHEN_STRIP_SCOPE,
180181
} from './validate-readonly-flow-writes.js';
181-
import { objectsAsServed, objectsWithoutTiedFields, recordsOf } from './object-graph.js';
182+
import { recordsOf } from './object-graph.js';
182183

183184
export type ReadonlyHookWriteSeverity = 'error' | 'warning';
184185

@@ -404,13 +405,10 @@ export function validateReadonlyHookWrites(
404405
);
405406
if (writes.length === 0) return;
406407

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)));
408+
// [#30471] The readonly index reads the objects as served
409+
// (`buildServedReadonlyIndex`). The insert-strip exemption is object-level
410+
// and stays on the declared objects.
411+
roIndex ??= buildServedReadonlyIndex(stack);
414412
insertStripExempt ??= buildInsertStripExemptObjects(recordsOf(stack.objects));
415413

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

‎packages/lint/src/validate-sortable-fields.ts‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ import {
256256
type FieldSortability,
257257
} from '@objectstack/spec/api';
258258

259-
import { recordsOf, suggestName } from './object-graph.js';
259+
import { objectsAsServed, objectsWithoutTiedFields, recordsOf, suggestName } from './object-graph.js';
260260
import { walkPageComponents } from './page-walk.js';
261261
import { PAGE_FIELD_UNKNOWN, PAGE_FIELD_UNPROVISIONED } from './validate-page-field-bindings.js';
262262
import {
@@ -512,6 +512,38 @@ export function indexObjectSortability(stack: AnyRec): Map<string, SortabilityFi
512512
return out;
513513
}
514514

515+
/**
516+
* [#30471] The two field indexes over the objects as SERVED (`objectsAsServed`,
517+
* the package's one fold), as the list door reads them:
518+
*
519+
* - existence ({@link indexObjectSearchTargets}' shape): a field an
520+
* `objectExtensions` entry adds is a known name, a field two packages tie
521+
* on included, since existence never asks which definition wins;
522+
* - sortability ({@link indexObjectSortability}): a question about the
523+
* field's definition, so a tied field is left out
524+
* (`objectsWithoutTiedFields`) and goes unjudged.
525+
*
526+
* An object that declares no field map keeps its declared answer on both
527+
* (skip 2, `null` and its own projection): an extension adding to an
528+
* introspected schema does not make that schema knowable, extended or not.
529+
*/
530+
function indexServedSortFields(stack: AnyRec): {
531+
fieldsByObject: Map<string, ObjectSearchTarget | null>;
532+
sortability: Map<string, SortabilityFields>;
533+
} {
534+
const served = objectsAsServed(stack);
535+
const fieldsByObject = indexObjectSearchTargets({ objects: served.objects });
536+
const sortability = indexObjectSortability({ objects: objectsWithoutTiedFields(served) });
537+
const declaredSortability = indexObjectSortability(stack);
538+
for (const [name, target] of indexObjectSearchTargets(stack)) {
539+
if (target !== null) continue;
540+
fieldsByObject.set(name, null);
541+
const own = declaredSortability.get(name);
542+
if (own) sortability.set(name, own);
543+
}
544+
return { fieldsByObject, sortability };
545+
}
546+
515547
/**
516548
* The finding for one sort key whose projection entry says `sortable: false`,
517549
* or `undefined` when it does not (no entry — unknown, dotted, a registry
@@ -787,12 +819,14 @@ export function validateSortableFields(stack: AnyRec): SortableFieldFinding[] {
787819
: isRec(stack.objects)
788820
? Object.entries(stack.objects).map(([name, def]) => ({ name, ...(def as AnyRec) }))
789821
: [];
790-
const fieldsByObject = indexObjectSearchTargets(stack);
822+
// [#30471] The existence index and the sortability projection read the
823+
// objects as served ({@link indexServedSortFields}); the walk above stays on
824+
// the declared objects, so every finding keeps its declared path.
825+
const { fieldsByObject, sortability } = indexServedSortFields(stack);
791826
// [#10474] The provenance index alongside the existence one — same keying,
792-
// asked on the path where the existence check stays silent.
827+
// asked on the path where the existence check stays silent. Object-level
828+
// (`external`, the injected anchors), so it stays on the declared objects.
793829
const unprovisionedAnchors = indexUnprovisionedAnchors(stack);
794-
// [#30023] The sortability projection the list door refuses by, per object.
795-
const sortability = indexObjectSortability(stack);
796830

797831
const check = (
798832
declared: unknown,

0 commit comments

Comments
 (0)