Skip to content

Commit 3c1725d

Browse files
committed
feat(lint): judge grouping.fields[].orderBy the way the aggregate door does
WIP: existence, a dotted path at the aggregate axis, and the new list-view-group-order-unorderable for a type min/max cannot order; explanations, barrel export, changeset, authorable surface. Claude-Session: https://claude.ai/code/session_01E1GNboWQRhh5vidwh2hxTY Co-authored-by: Claude <noreply@anthropic.com>
1 parent eb0ade2 commit 3c1725d

6 files changed

Lines changed: 272 additions & 5 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@objectstack/spec': minor
3+
'@objectstack/lint': minor
4+
---
5+
6+
feat(spec,lint): a list-view grouping level can order its groups by another field (`grouping.fields[].orderBy`)
7+
8+
Clause-②: yes
9+
Metadata-protocol: added
10+
11+
- **Before.** A grouping level ordered its groups only by its own key. A period label grouped as text (`Nov 2026`, `Oct 2026`, `Q1 2027`, `Q4 2025`), so putting periods in time order took a second, outer grouping level on the period start, which showed two header rows for every group.
12+
- **Now.** `GroupingFieldSchema` gains `orderBy`, the name of a field on the same object whose value orders this level's groups: `{ field: 'period_label', orderBy: 'period_start', order: 'desc' }`. When it is absent, or names the grouping field itself, the groups order by their own key as before. Nothing is defaulted in.
13+
- **What the header query carries.** The consumer still sorts the header rows. `compileListViewGroupQuery` now adds the value to sort by: one `min` node over `orderBy` for `order: 'asc'`, or one `max` node for `'desc'`, under `min_<field>` / `max_<field>`. Each group sits where its first row would sit if the rows were sorted by `orderBy` in `order`. The grouping key stays the only `groupBy` column, so there is one header per label. A column summary that already declares the same `min` is the same node and the same column. In a multi-level grouping the outer level's value folds exactly from the leaf rows (the `min` of mins, the `max` of maxes).
14+
- **`listViewGroupOrderColumn(grouping, level)`** (`@objectstack/spec/ui`) names the header-row column a level sorts by: the grouping field itself, or the order node's alias. A consumer reads this instead of rebuilding the alias. A group whose order value is `null` sorts last in both directions, and equal values fall back to the group key.
15+
- **Refused, at compile and at authoring.** A blank `orderBy` is refused by the compiler (`grouping_order_by_blank`, `INVALID_QUERY` / 400) and by the schema (`too_small`). An order alias that lands on a grouped field's own column is `alias_collision`. `os validate`, `os build` and the runtime save door judge `orderBy` the way the aggregate door does. An unknown name is `list-view-field-unknown`. A dotted path is `list-view-field-dotted`, because the header query compiles no join. A field whose type `min` / `max` cannot order on every backend is the new `list-view-group-order-unorderable` (`error`). That covers the string classes, whose order depends on the collation, and the virtual `formula`. Each of these is refused `400 INVALID_FIELD` by `engine.aggregate`, so the grouped view would render no group.
16+
- **Unchanged.** `order` and `collapsed` keep their meaning and defaults. The row page query (`compileListViewGroupRowsQuery`) does not change. `EngineAggregateOptions` gains no `orderBy`, because the group sort stays the consumer's. The new key is purely additive, so a grouping declared today compiles to exactly the same query.

‎packages/lint/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,13 @@ export type {
521521
// refused the same way; `wholeCollection` is the statement.
522522
// Its FIFTH: a list view still carrying a key the spec retired, reported by
523523
// name with the tombstone's prescription (`os lint` never parses).
524+
// [#25786] Its SIXTH: a grouping level's `orderBy` naming a field the group
525+
// header query cannot order by (`min` / `max` refused for its type).
524526
export {
525527
validateListViewFieldRefs,
526528
LIST_VIEW_FIELD_UNKNOWN,
527529
LIST_VIEW_FIELD_DOTTED,
530+
LIST_VIEW_GROUP_ORDER_UNORDERABLE,
528531
LIST_VIEW_OBJECT_OUTSIDE_CLOSURE,
529532
LIST_VIEW_OBJECT_UNKNOWN,
530533
LIST_VIEW_KEY_RETIRED,

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,14 +3278,15 @@ const LIST_VIEW_FIELD_UNKNOWN_EXPLANATION: RuleExplanation = {
32783278
// The positions are held to `listViewWalkedPositions()` by the rule's test.
32793279
'Every field a list view names must be a field of its bound object: its `columns` (and a ' +
32803280
'column\'s `summary` and `prefix`), `hiddenFields`, `fieldOrder`, ' +
3281-
'`grouping`, `rowColor` and `userFilters`, the keys of its `filter` and of each ' +
3281+
'`grouping` (and a grouping level\'s `orderBy`), `rowColor` and `userFilters`, the keys of its `filter` and of each ' +
32823282
'`userFilters.tabs[].filter`, and every binding inside a `kanban`, `calendar`, `gantt`, ' +
32833283
'`timeline`, `gallery`, `map` or `tree` block. Nothing resolves the name at render time: the ' +
32843284
'view renders successfully with a blank, empty or wrong result, and nothing else reports the ' +
32853285
'miss. The verdict\'s "Did you mean" names the nearest field the object declares.',
32863286
'An error where the miss changes what the view returns or collapses what it lays out — for ' +
32873287
'example, a filter key, user filter or gantt quick filter matches no record; a column is sent as the ' +
3288-
'projection, where the REST ingress answers `400 INVALID_FIELD`; a `kanban.groupByField` ' +
3288+
'projection, where the REST ingress answers `400 INVALID_FIELD`; a grouping level\'s `orderBy` ' +
3289+
'is an operand of the group header query, which the aggregate door refuses the same way; a `kanban.groupByField` ' +
32893290
'collapses every card into one lane; a required date field of a `gantt`, `calendar` or ' +
32903291
'`timeline` leaves nothing to place; a map with no coordinate field plots no marker; a ' +
32913292
'`tree.parentField` flattens the tree; a `gantt.lockField` unfreezes every row and a ' +
@@ -3323,6 +3324,10 @@ const LIST_VIEW_FIELD_DOTTED_EXPLANATION: RuleExplanation = {
33233324
'(`assertFilterIsMaterializable` and the REST ingress, `400 INVALID_FIELD`) rather than ' +
33243325
'answered with an empty list. A structured or JSON head, an array (`multiple: true`) head, a ' +
33253326
'file head and an unreadable head are served by the door, so they are not reported.',
3327+
'Aggregate: a grouping level\'s `orderBy` is the operand of the `min` / `max` node the group ' +
3328+
'header query orders its groups by. The header query compiles no join, so the aggregate door ' +
3329+
'refuses every dotted operand whatever its head (`400 INVALID_FIELD`), and the grouped view ' +
3330+
'renders no group.',
33263331
'The ways out are the doors\' own. At a projection position, name the whole column and read ' +
33273332
'into its value in the client, or read the related record with `expand`; at a filter position, ' +
33283333
'filter on a column of the bound object itself. At either, denormalise the value onto the bound ' +
@@ -3334,6 +3339,26 @@ const LIST_VIEW_FIELD_DOTTED_EXPLANATION: RuleExplanation = {
33343339
],
33353340
};
33363341

3342+
const LIST_VIEW_GROUP_ORDER_UNORDERABLE_EXPLANATION: RuleExplanation = {
3343+
rule: 'list-view-group-order-unorderable',
3344+
covers: 'which fields a grouping level can order its groups by',
3345+
paragraphs: [
3346+
'A grouping level\'s `orderBy` orders its groups by another field\'s value — a period label in time ' +
3347+
'order by its period start. The group header query carries that value per group: the smallest ' +
3348+
'(`min`) for `order: asc`, the largest (`max`) for `desc`. So the field must be one `min` and `max` ' +
3349+
'order the same way on every backend: the aggregate × field-type table (`AGGREGATE_FIELD_TYPE_COMPATIBILITY` ' +
3350+
'in `@objectstack/spec/data`) accepts the numeric, temporal and boolean classes.',
3351+
'It refuses the string classes — text, option values, references and `autonumber` — because two ' +
3352+
'backends order strings by two collations, and the virtual `formula`, which stores no column. The ' +
3353+
'engine\'s aggregate door asks the same table, so such an `orderBy` gets the grouped view\'s header ' +
3354+
'query refused (`400 INVALID_FIELD`) and no group renders. To order text labels, store each label\'s ' +
3355+
'rank in a number field and order by that.',
3356+
'Not judged: a level whose `orderBy` names its own grouping field (it compiles no node), a field with no ' +
3357+
'declared type, and a declared type outside `FieldType` — the aggregate door judges neither. A name ' +
3358+
'that resolves to nothing is `list-view-field-unknown`; a dotted one is `list-view-field-dotted`.',
3359+
],
3360+
};
3361+
33373362
/** Which keys a list view's binding is read from, and what stays unjudged (both binding ids write it out). */
33383363
const LIST_VIEW_BINDING_SCOPE =
33393364
'One finding per list view, at the key the binding was read from: the view\'s `object`, or a list ' +
@@ -5010,6 +5035,7 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
50105035
[FILTER_EMPTY_NODE_EXPLANATION.rule]: FILTER_EMPTY_NODE_EXPLANATION,
50115036
[LIST_VIEW_FIELD_UNKNOWN_EXPLANATION.rule]: LIST_VIEW_FIELD_UNKNOWN_EXPLANATION,
50125037
[LIST_VIEW_FIELD_DOTTED_EXPLANATION.rule]: LIST_VIEW_FIELD_DOTTED_EXPLANATION,
5038+
[LIST_VIEW_GROUP_ORDER_UNORDERABLE_EXPLANATION.rule]: LIST_VIEW_GROUP_ORDER_UNORDERABLE_EXPLANATION,
50135039
[LIST_VIEW_OBJECT_OUTSIDE_CLOSURE_EXPLANATION.rule]: LIST_VIEW_OBJECT_OUTSIDE_CLOSURE_EXPLANATION,
50145040
[LIST_VIEW_OBJECT_UNKNOWN_EXPLANATION.rule]: LIST_VIEW_OBJECT_UNKNOWN_EXPLANATION,
50155041
[TRANSLATION_TARGET_UNKNOWN_EXPLANATION.rule]: TRANSLATION_TARGET_UNKNOWN_EXPLANATION,

‎packages/lint/src/validate-list-view-field-refs.test.ts‎

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
validateListViewFieldRefs as validateListViewFieldRefsUnrecorded,
1717
LIST_VIEW_FIELD_UNKNOWN,
1818
LIST_VIEW_FIELD_DOTTED,
19+
LIST_VIEW_GROUP_ORDER_UNORDERABLE,
1920
LIST_VIEW_OBJECT_OUTSIDE_CLOSURE,
2021
LIST_VIEW_OBJECT_UNKNOWN,
2122
listViewWalkedPositions,
@@ -92,7 +93,7 @@ const FULL_LIST_VIEW: AnyRec = {
9293
filter: [{ field: 'visible_from', operator: 'equals', value: '2026-01-01' }],
9394
hiddenFields: ['business_unit'],
9495
fieldOrder: ['title', 'status'],
95-
grouping: { fields: [{ field: 'business_unit' }] },
96+
grouping: { fields: [{ field: 'business_unit', orderBy: 'due_at' }] },
9697
rowColor: { field: 'status' },
9798
userFilters: {
9899
fields: [{ field: 'status' }],
@@ -298,6 +299,13 @@ describe('#14107 — every other walked position', () => {
298299
],
299300
[{ hiddenFields: [BAD] }, 'views[0].list.hiddenFields[0]', 'warning'],
300301
[{ fieldOrder: [BAD] }, 'views[0].list.fieldOrder[0]', 'warning'],
302+
// [#25786] An operand of the group header query: the aggregate door
303+
// refuses an unknown column, so no group renders.
304+
[
305+
{ grouping: { fields: [{ field: 'business_unit', orderBy: BAD }] } },
306+
'views[0].list.grouping.fields[0].orderBy',
307+
'error',
308+
],
301309
[{ rowColor: { field: BAD } }, 'views[0].list.rowColor.field', 'warning'],
302310
[{ userFilters: { fields: [{ field: BAD }] } }, 'views[0].list.userFilters.fields[0].field', 'error'],
303311
[
@@ -1132,6 +1140,109 @@ describe('[#22161] one-line verdicts — list-view-field-unknown, list-view-fiel
11321140
});
11331141
});
11341142

1143+
/**
1144+
* [#25786] `grouping.fields[].orderBy` reaches a door — the group header
1145+
* query's `min` / `max` node, executed by `engine.aggregate` — so all three of
1146+
* that door's verdicts are this position's: the name resolves, it is not a
1147+
* dotted path, and `min` / `max` over its declared type is a pair the
1148+
* aggregate × field-type table accepts.
1149+
*/
1150+
describe('[#25786] a grouping level ordered by another field', () => {
1151+
const ordered = (orderBy: string, extra: AnyRec = {}): AnyRec => ({
1152+
grouping: { fields: [{ field: 'business_unit', orderBy, ...extra }] },
1153+
});
1154+
const findingsFor = (patch: AnyRec) => validateListViewFieldRefs(stackWith(mutate(patch)));
1155+
1156+
it.each([
1157+
['a date field', 'due_at'],
1158+
['a number field', 'estimate'],
1159+
['a boolean field', 'is_locked'],
1160+
['a registry-injected datetime column', 'created_at'],
1161+
])('orders by %s — silent', (_label, orderBy) => {
1162+
expect(findingsFor(ordered(orderBy))).toEqual([]);
1163+
expect(findingsFor(ordered(orderBy, { order: 'desc' }))).toEqual([]);
1164+
});
1165+
1166+
it.each([
1167+
['text', 'title', 'min'],
1168+
['select', 'status', 'min'],
1169+
['lookup', 'owner', 'max'],
1170+
])('a `%s` field is unorderable — `error`, naming the refused pair, at the level\'s `orderBy`', (type, orderBy, fn) => {
1171+
const findings = findingsFor(ordered(orderBy, fn === 'max' ? { order: 'desc' } : {}));
1172+
expect(findings).toHaveLength(1);
1173+
expect(findings[0].rule).toBe(LIST_VIEW_GROUP_ORDER_UNORDERABLE);
1174+
expect(findings[0].severity).toBe('error');
1175+
expect(findings[0].path).toBe('views[0].list.grouping.fields[0].orderBy');
1176+
expect(findings[0].message).toContain(`is a \`${type}\` field, and \`${fn}\` over it is refused`);
1177+
});
1178+
1179+
it('a virtual `formula` field carries the spec\'s store-it remedy, not the generic one', () => {
1180+
const [finding, ...rest] = findingsFor(ordered('score'));
1181+
expect(rest).toEqual([]);
1182+
expect(finding.rule).toBe(LIST_VIEW_GROUP_ORDER_UNORDERABLE);
1183+
expect(finding.hint).toMatch(/^The field is computed on read and stores no value/);
1184+
});
1185+
1186+
it('a dotted `orderBy` is refused at the aggregate door, whatever its head', () => {
1187+
const findings = findingsFor(ordered('owner.name'));
1188+
expect(findings.map((f) => [f.rule, f.path, f.severity])).toEqual([
1189+
[LIST_VIEW_FIELD_DOTTED, 'views[0].list.grouping.fields[0].orderBy', 'error'],
1190+
]);
1191+
expect(findings[0].message).toContain('the group header query\'s aggregation refuses it');
1192+
// A head that resolves to nothing is the unknown class, as everywhere.
1193+
expect(findingsFor(ordered('ownr.name')).map((f) => f.rule)).toEqual([LIST_VIEW_FIELD_UNKNOWN]);
1194+
});
1195+
1196+
it('ordering by its own grouping field compiles no node — nothing is judged at `orderBy`', () => {
1197+
// A text grouping field ordering by itself is the default, not a `min` over text.
1198+
expect(findingsFor(ordered('business_unit'))).toEqual([]);
1199+
// And a dangling grouping field is reported ONCE, at `field`.
1200+
const dangling = findingsFor({ grouping: { fields: [{ field: 'nope_field', orderBy: 'nope_field' }] } });
1201+
expect(idsOf(dangling)).toEqual(['views[0].list.grouping.fields[0].field']);
1202+
});
1203+
1204+
it('a field with no declared type, or a type outside `FieldType`, is not judged — the door judges neither', () => {
1205+
// `id` is driver-provisioned: the graph knows it and has no type for it.
1206+
expect(findingsFor(ordered('id'))).toEqual([]);
1207+
const objects = [{ ...OBJECTS[0], fields: [...OBJECTS[0].fields, { name: 'rank_raw', type: 'integer', label: 'Rank' }] }, OBJECTS[1]];
1208+
expect(validateListViewFieldRefs(stackWith(mutate(ordered('rank_raw')), objects))).toEqual([]);
1209+
});
1210+
1211+
it('every level is judged, each at its own index', () => {
1212+
const findings = findingsFor({
1213+
grouping: { fields: [{ field: 'status', orderBy: 'estimate' }, { field: 'business_unit', orderBy: 'title' }] },
1214+
});
1215+
expect(findings.map((f) => [f.rule, f.path])).toEqual([
1216+
[LIST_VIEW_GROUP_ORDER_UNORDERABLE, 'views[0].list.grouping.fields[1].orderBy'],
1217+
]);
1218+
});
1219+
1220+
for (const command of ['validate', 'build'] as const) {
1221+
it(`an unorderable \`orderBy\` fails \`${command}\``, () => {
1222+
const { errors } = splitBySeverity(runAuthoringRules(command, { normalized: stackWith(mutate(ordered('title'))) }));
1223+
expect(errors.map((f) => f.rule)).toContain(LIST_VIEW_GROUP_ORDER_UNORDERABLE);
1224+
});
1225+
}
1226+
1227+
it('reaches the runtime save door through the reference-integrity suite', () => {
1228+
const findings = validateReferenceIntegrity(stackWith(mutate(ordered('title'))), { runtimeWriteType: 'view' });
1229+
expect(findings.map((f) => f.rule)).toContain(LIST_VIEW_GROUP_ORDER_UNORDERABLE);
1230+
});
1231+
1232+
it('`os explain` carries the reasoning, and the verdict is one line', () => {
1233+
const entry = explainRule(LIST_VIEW_GROUP_ORDER_UNORDERABLE);
1234+
expect(entry, 'no `os explain` entry').toBeDefined();
1235+
const text = entry!.paragraphs.join('\n');
1236+
for (const fact of ['`min`', '`max`', 'collation', '`formula`', 'rank in a number field']) {
1237+
expect(text, fact).toContain(fact);
1238+
}
1239+
for (const f of fired.filter((x) => x.rule === LIST_VIEW_GROUP_ORDER_UNORDERABLE)) {
1240+
expect(f.hint).not.toContain('\n');
1241+
expect(f.hint.length, f.hint).toBeLessThanOrEqual(200);
1242+
}
1243+
});
1244+
});
1245+
11351246
// [#22161] Slice 16: the `fix:` lines. The unknown id prints its remedy alone
11361247
// (the verdict's "Did you mean" names the nearest field, so the object's field
11371248
// list is no longer appended); the dotted id prints one remedy line per door,

0 commit comments

Comments
 (0)