Skip to content

Commit 5d64c37

Browse files
committed
fix(spec,lint): refuse a padded grouping orderBy; name the two server doors; state the whole-set sort
An orderBy name is compiled verbatim into the header query's min / max node, so a padded spelling is refused by name, as a padded grouping field is. The unknown / dotted refusals are the data door's ingress gate, the type refusal is engine.aggregate's type door; the prose now says which. The header set is sorted whole: a limit on the header query slices before any sort. Equal order values fall back to the group key in the same direction. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E1GNboWQRhh5vidwh2hxTY
1 parent 5032316 commit 5d64c37

7 files changed

Lines changed: 108 additions & 35 deletions

File tree

‎.changeset/25786-grouping-order-by.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Metadata-protocol: added
1111
- **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.
1212
- **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.
1313
- **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.
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 in the same direction.
15+
- **Refused, at compile and at authoring.** An empty `orderBy` is refused by the schema (`too_small`), and a padded one, whitespace-only included, by name, as a padded `field` is, because the name is compiled verbatim. The compiler refuses a blank one in-process (`grouping_order_by_blank`, `INVALID_QUERY` / 400). 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 server doors do. 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, the virtual `formula`, and every other type outside the numeric, temporal and boolean classes. Each of these is refused `400 INVALID_FIELD` on the server, so the grouped view would render no group: an unknown or dotted name by the data door's ingress (`findData`), and an unorderable type by `engine.aggregate`'s type door.
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. That sort is right only over the whole header set, which is what the compiler asks for: a `limit` on the header query is sliced by `findData` before any sort, so a paged header set honours neither `order` nor `orderBy`, the limit already recorded on `GroupingConfigSchema`. The new key is purely additive, so a grouping declared today compiles to exactly the same query.

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ const LIST_VIEW_FIELD_UNKNOWN_EXPLANATION: RuleExplanation = {
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 ' +
32883288
'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` ' +
3289+
'is an operand of the group header query, which the data door refuses the same way; a `kanban.groupByField` ' +
32903290
'collapses every card into one lane; a required date field of a `gantt`, `calendar` or ' +
32913291
'`timeline` leaves nothing to place; a map with no coordinate field plots no marker; a ' +
32923292
'`tree.parentField` flattens the tree; a `gantt.lockField` unfreezes every row and a ' +
@@ -3325,9 +3325,9 @@ const LIST_VIEW_FIELD_DOTTED_EXPLANATION: RuleExplanation = {
33253325
'answered with an empty list. A structured or JSON head, an array (`multiple: true`) head, a ' +
33263326
'file head and an unreadable head are served by the door, so they are not reported.',
33273327
'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.',
3328+
'header query orders its groups by. The header query compiles no join, so the data door\'s ' +
3329+
'ingress refuses every dotted operand whatever its head (`assertAggregationFieldsExist`, ' +
3330+
'`400 INVALID_FIELD`), and the grouped view renders no group.',
33313331
'The ways out are the doors\' own. At a projection position, name the whole column and read ' +
33323332
'into its value in the client, or read the related record with `expand`; at a filter position, ' +
33333333
'filter on a column of the bound object itself. At either, denormalise the value onto the bound ' +
@@ -3348,11 +3348,12 @@ const LIST_VIEW_GROUP_ORDER_UNORDERABLE_EXPLANATION: RuleExplanation = {
33483348
'(`min`) for `order: asc`, the largest (`max`) for `desc`. So the field must be one `min` and `max` ' +
33493349
'order the same way on every backend: the aggregate × field-type table (`AGGREGATE_FIELD_TYPE_COMPATIBILITY` ' +
33503350
'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.',
3351+
'It refuses every other type, among them the string classes — text, option values, references and ' +
3352+
'`autonumber` — because two backends order strings by two collations, the virtual `formula`, which ' +
3353+
'stores no column, and the file, structured and vector types. The engine\'s aggregate door (`engine.aggregate`) asks ' +
3354+
'the same table, so such an `orderBy` gets the grouped view\'s header query refused ' +
3355+
'(`400 INVALID_FIELD`) and no group renders. To order text labels, store each label\'s rank in a ' +
3356+
'number field and order by that.',
33563357
'Not judged: a level whose `orderBy` names its own grouping field (it compiles no node), a field with no ' +
33573358
'declared type, and a declared type outside `FieldType` — the aggregate door judges neither. A name ' +
33583359
'that resolves to nothing is `list-view-field-unknown`; a dotted one is `list-view-field-dotted`.',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ describe('[#25786] a grouping level ordered by another field', () => {
11831183
expect(finding.hint).toMatch(/^The field is computed on read and stores no value/);
11841184
});
11851185

1186-
it('a dotted `orderBy` is refused at the aggregate door, whatever its head', () => {
1186+
it('a dotted `orderBy` is refused at the data door\'s ingress, whatever its head', () => {
11871187
const findings = findingsFor(ordered('owner.name'));
11881188
expect(findings.map((f) => [f.rule, f.path, f.severity])).toEqual([
11891189
[LIST_VIEW_FIELD_DOTTED, 'views[0].list.grouping.fields[0].orderBy', 'error'],

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,15 @@
312312
* A grouping level's `orderBy` names the field whose value orders its groups.
313313
* Unlike the renderer bindings, it REACHES A DOOR: the group header query
314314
* (`compileListViewGroupQuery`) carries one `min` / `max` node over it, which
315-
* `engine.aggregate` executes. Three verdicts, each the door's own:
315+
* `engine.aggregate` executes. Three verdicts, each the door's own — two
316+
* doors, both `400 INVALID_FIELD`:
316317
*
317-
* - **Existence** — {@link LIST_VIEW_FIELD_UNKNOWN}, `error`: the aggregate
318-
* door refuses an unknown column `400 INVALID_FIELD`, so the grouped view
319-
* renders no group at all.
318+
* - **Existence** — {@link LIST_VIEW_FIELD_UNKNOWN}, `error`: the data
319+
* door's ingress (`findData`'s `assertAggregationFieldsExist`) refuses an
320+
* unknown operand, so the grouped view renders no group at all.
320321
* - **A dotted path** — {@link LIST_VIEW_FIELD_DOTTED} at the `aggregate`
321-
* axis: the header query compiles no join, and the door refuses every
322-
* dotted aggregation operand by name, whatever its head.
322+
* axis: the header query compiles no join, and the same ingress gate
323+
* refuses every dotted aggregation operand, whatever its head.
323324
* - **Orderability** — {@link LIST_VIEW_GROUP_ORDER_UNORDERABLE}, `error`:
324325
* the pair `min` / `max` × the field's declared type, asked of the spec's
325326
* table (`isAggregateCompatibleWithFieldType`) exactly as the engine's

‎packages/spec/src/ui/view-grouping-query.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,23 @@
139139
* their maxes — so an outer level's order value is derived from the
140140
* leaf rows like its `count`.
141141
* * **The operand's type** is the aggregate × field-type table's: `min` /
142-
* `max` accept the numeric, temporal and boolean classes and refuse the
143-
* string classes (collation-dependent order). The engine's aggregate door
144-
* refuses a refused pair with `INVALID_FIELD` / 400; `os validate` refuses
145-
* it at authoring (`list-view-group-order-unorderable`). This module has
146-
* no field types and judges nothing about them.
142+
* `max` accept the numeric, temporal and boolean classes and refuse every
143+
* other (the string classes for their collation-dependent order, the
144+
* virtual `formula`). Two doors refuse with `INVALID_FIELD` / 400: the
145+
* data door's ingress (`findData`'s field-existence gate) an unknown or
146+
* dotted name, and `engine.aggregate`'s type door a refused type. `os
147+
* validate` refuses all three at authoring. This module has no field
148+
* types and judges nothing about them.
147149
* * **What the consumer owes** is stated on `GroupingFieldSchema.orderBy`: a
148150
* group whose order value is `null` (every row empty there) sorts last
149-
* in both directions, and equal values fall back to the group key.
151+
* in both directions, and equal values fall back to the group key in the
152+
* same direction.
153+
* * **The header set is sorted whole.** The header query is compiled
154+
* unpaged, and the consumer's sort is right only over every group: a
155+
* `limit` on it is sliced by `findData` in the engine's emission order,
156+
* before any sort, so a paged header set honours neither `order` nor
157+
* `orderBy` — the recorded "Header cardinality is unbounded" limit on
158+
* `GroupingConfigSchema`, unchanged here.
150159
*
151160
* ## The door — existing, not new
152161
*

‎packages/spec/src/ui/view.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,33 @@ describe('GroupingFieldSchema', () => {
23002300
expect(result.error!.issues.map((i) => [i.path.join('.'), i.code])).toEqual([['orderBy', code]]);
23012301
});
23022302

2303+
// The name is compiled into the `min` / `max` node verbatim, as `field` is
2304+
// into `groupBy`, so a padded spelling is refused the same way (#17360's
2305+
// ruling), not trimmed: it would name a column no record has.
2306+
it.each([
2307+
['leading', ' period_start', 'Write "period_start".'],
2308+
['trailing', 'period_start ', 'Write "period_start".'],
2309+
['a tab', '\tperiod_start', 'Write "period_start".'],
2310+
['whitespace only', ' ', 'nothing but whitespace'],
2311+
])('refuses %s whitespace at `orderBy`, by name', (_label, spelling, remedy) => {
2312+
const result = GroupingFieldSchema.safeParse({ field: 'period_label', orderBy: spelling });
2313+
expect(result.success).toBe(false);
2314+
expect(result.error!.issues.map((i) => [i.path.join('.'), i.code])).toEqual([['orderBy', 'custom']]);
2315+
const message = result.error!.issues[0]!.message;
2316+
expect(message).toContain('grouping.fields[].orderBy');
2317+
expect(message).toContain(JSON.stringify(spelling));
2318+
expect(message).toContain(remedy);
2319+
});
2320+
2321+
it('refuses a padded `orderBy` through ListViewSchema at `grouping.fields[N].orderBy`', () => {
2322+
const result = ListViewSchema.safeParse({
2323+
columns: ['name'],
2324+
grouping: { fields: [{ field: 'status' }, { field: 'period_label', orderBy: 'period_start ' }] },
2325+
});
2326+
expect(result.success).toBe(false);
2327+
expect(result.error!.issues.map((i) => i.path.join('.'))).toEqual(['grouping.fields.1.orderBy']);
2328+
});
2329+
23032330
// ==========================================================================
23042331
// [#17360] A padded `field` is REFUSED, not trimmed
23052332
// (objectui#7347 ruling C, decision batch #110 item 5 — refuse at the producer)

‎packages/spec/src/ui/view.zod.ts‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,30 @@ function checkGroupingFieldName(raw: string): string | undefined {
12801280
+ `statement about the data. ${remedy}`;
12811281
}
12821282

1283+
/**
1284+
* Validate one `grouping.fields[].orderBy` name (#25786): the same non-padded
1285+
* rule as {@link checkGroupingFieldName}, for the same reason — the name is
1286+
* compiled into the group header query verbatim, here as a `min` / `max`
1287+
* operand. Returns the author-facing refusal, or `undefined` when it conforms.
1288+
* The empty string is the schema's `min(1)` and the compiler's
1289+
* `grouping_order_by_blank`, not this.
1290+
*/
1291+
function checkGroupingOrderByName(raw: string): string | undefined {
1292+
if (NON_PADDED_FIELD_NAME_PATTERN.test(raw)) return undefined;
1293+
1294+
const trimmed = raw.trim();
1295+
const remedy = trimmed === ''
1296+
? 'Name the field to order the groups by, or remove `orderBy` — this value is nothing but whitespace.'
1297+
: `Write ${JSON.stringify(trimmed)}.`;
1298+
1299+
return '`grouping.fields[].orderBy` names the field exactly as it is stored, with no leading or '
1300+
+ `trailing whitespace — received ${JSON.stringify(raw)}. The group header query is compiled `
1301+
+ 'from this string verbatim (`compileListViewGroupQuery` sends it as the operand of the level\'s '
1302+
+ '`min` / `max` node), so a padded spelling names a column no record has: the data door refuses '
1303+
+ 'the header query `400 INVALID_FIELD` and the grouped view renders no group. '
1304+
+ remedy;
1305+
}
1306+
12831307
/*
12841308
* ---------------------------------------------------------------------------
12851309
* `groupByField` — the same non-padded name rule, three more schemas (#17499)
@@ -1384,7 +1408,11 @@ const groupByFieldCheck = (view: GroupByFieldView) => (raw: string, ctx: z.Refin
13841408
* field-type table (`AGGREGATE_FIELD_TYPE_COMPATIBILITY`, the `min` / `max`
13851409
* row): a numeric, temporal or boolean field — the table refuses the string
13861410
* classes because two backends order strings by two collations — and
1387-
* `os validate` refuses any other (`list-view-group-order-unorderable`).
1411+
* `os validate` refuses any other (`list-view-group-order-unorderable`). The
1412+
* name is compiled verbatim, so a padded one is refused here as `field`'s is
1413+
* (#17360's ruling, {@link checkGroupingOrderByName}). The header set is
1414+
* sorted whole: a `limit` on the header query slices before any sort (the
1415+
* "Header cardinality is unbounded" limit on {@link GroupingConfigSchema}).
13881416
*/
13891417
export const GroupingFieldSchema = lazySchema(() => strictObject({
13901418
surface: 'this grouping field',
@@ -1405,15 +1433,22 @@ export const GroupingFieldSchema = lazySchema(() => strictObject({
14051433
'Group sort order — applied by the consumer over the header rows (the aggregate query itself sorts '
14061434
+ 'nothing): by the group key, or by the `orderBy` value each header row carries when `orderBy` is set',
14071435
),
1408-
orderBy: z.string().min(1).optional().describe(
1409-
'Field whose value orders this level\'s groups, when that is not the grouping field — a field of the '
1410-
+ 'same object; default: the grouping field itself, so the groups order by their own key. The group '
1411-
+ 'header query then carries, per group, the smallest value of this field for `order: asc` and the '
1412-
+ 'largest for `desc` (each group sits where its first row would if the rows were sorted by this field), '
1413-
+ 'and the consumer sorts the header rows by it; a group with no value here sorts last in both '
1414-
+ 'directions, and equal values fall back to the group key. A numeric, temporal or boolean field: '
1415-
+ 'min/max over a string orders by collation, which differs between backends, so the server refuses it',
1416-
),
1436+
orderBy: z.string().min(1)
1437+
.superRefine((raw, ctx) => {
1438+
const refusal = checkGroupingOrderByName(raw);
1439+
if (refusal) ctx.addIssue({ code: 'custom', message: refusal });
1440+
})
1441+
.optional()
1442+
.describe(
1443+
'Field whose value orders this level\'s groups, when that is not the grouping field — a field of the '
1444+
+ 'same object; default: the grouping field itself, so the groups order by their own key. The group '
1445+
+ 'header query then carries, per group, the smallest value of this field for `order: asc` and the '
1446+
+ 'largest for `desc` (each group sits where its first row would if the rows were sorted by this field), '
1447+
+ 'and the consumer sorts the header rows by it; a group with no value here sorts last in both '
1448+
+ 'directions, and equal values fall back to the group key in the same direction. NO leading or '
1449+
+ 'trailing whitespace (compiled verbatim, as `field` is). A numeric, temporal or boolean field: '
1450+
+ 'min/max over a string orders by collation, which differs between backends, so the server refuses it',
1451+
),
14171452
collapsed: z.boolean().default(false).describe('Collapse groups by default (presentation only)'),
14181453
}));
14191454

0 commit comments

Comments
 (0)