diff --git a/.changeset/38433-kanban-group-hint.md b/.changeset/38433-kanban-group-hint.md new file mode 100644 index 00000000000..fdb3126ad69 --- /dev/null +++ b/.changeset/38433-kanban-group-hint.md @@ -0,0 +1,8 @@ +--- +'@objectstack/lint': patch +--- + +Clause-②: no +Metadata-protocol: none + +`list-view-kanban-group-field-uncategorical`: the hint offers a grid grouping only for a stored, single-valued field. A board grouped by a multi-valued field, a JSON-stored field (`json`, `address`, `composite` and the like) or a `formula` field is now pointed at a grid column instead, because the engine refuses to group a list by one of those (`400 INVALID_FIELD`); following the old hint produced a grid that passed `os validate` and opened on that error. `os explain` for the rule says the same, branch by branch. diff --git a/packages/lint/src/rule-explanations.ts b/packages/lint/src/rule-explanations.ts index 2584f6eff71..aa32d6f65a6 100644 --- a/packages/lint/src/rule-explanations.ts +++ b/packages/lint/src/rule-explanations.ts @@ -1558,7 +1558,10 @@ const LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL_EXPLANATION: RuleExplanation = 'declaration. Not judged: a dotted `groupByField`, one that names no field (`list-view-field-unknown`), ' + 'and a field whose type is not readable.', 'To fix it, group the board by a single-valued `select` field (one lane per option), a boolean or a ' + - 'lookup; or show the field as a grid column or grouping instead of board lanes.', + 'lookup; or show the field in a grid instead of board lanes. A stored, single-valued field (text, ' + + 'number, date) can be a grid grouping. A multi-valued field, a JSON-stored field (`json`, `address`, ' + + '`composite` and the like) or a `formula` field can only be a grid column: the engine refuses to group ' + + 'a list by one (`400 INVALID_FIELD`), so a grid grouping on it opens on an error.', ], }; diff --git a/packages/lint/src/validate-list-view-field-refs.ts b/packages/lint/src/validate-list-view-field-refs.ts index d21d3822944..2cfed8e597e 100644 --- a/packages/lint/src/validate-list-view-field-refs.ts +++ b/packages/lint/src/validate-list-view-field-refs.ts @@ -506,6 +506,7 @@ import { BOOLEAN_VALUE_TYPES, FieldType, REFERENCE_VALUE_TYPES, + STRUCTURED_JSON_TYPES, classifyDottedFilterHead, isAggregateCompatibleWithFieldType, isMultiValueField, @@ -1471,6 +1472,20 @@ export function validateListViewFieldRefs( const { type, multiple } = verdict.meta; const multi = isMultiValueField({ type, multiple } as never); if (!multi && KANBAN_CATEGORICAL_TYPES.has(type)) return; + // The grid remedy is offered only where it holds. A list view `grouping` + // compiles to the group-header query the grid sends to `engine.aggregate`, + // which refuses a `groupBy` over a multi-value or structured-JSON field + // (`assertGroupByNamesNoJsonStoredField`) and over a virtual `formula` + // field (`assertGroupByTargetsAreStored`), `400 INVALID_FIELD`. Those three + // classes, read from the same spec predicates, get a grid column instead. + const ungroupable = multi + ? 'a multi-valued field' + : STRUCTURED_JSON_TYPES.has(type) + ? 'a JSON-stored field' + : isVirtualSearchField({ type } as never) ? 'a field computed on read' : null; + const gridRemedy = ungroupable === null + ? `show "${groupBy}" as a grid grouping` + : `show "${groupBy}" as a grid column (a list cannot be grouped by ${ungroupable})`; findings.push({ severity: 'warning', rule: LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL, @@ -1482,8 +1497,7 @@ export function validateListViewFieldRefs( : `"${object}.${groupBy}" is a \`${type}\` field: the board draws a lane per distinct value on its loaded ` + 'cards, none for a value no card holds, and none on an empty list', hint: - `Group the board by a single-valued \`select\` (one lane per option), a boolean or a lookup, or show ` + - `"${groupBy}" as a grid grouping`, + `Group the board by a single-valued \`select\` (one lane per option), a boolean or a lookup, or ${gridRemedy}`, }); }; diff --git a/packages/lint/src/validate-list-view-kanban-lanes.test.ts b/packages/lint/src/validate-list-view-kanban-lanes.test.ts index aee06f3d800..9c1edac31a9 100644 --- a/packages/lint/src/validate-list-view-kanban-lanes.test.ts +++ b/packages/lint/src/validate-list-view-kanban-lanes.test.ts @@ -165,6 +165,9 @@ describe('#35143 — a board grouped by a field with no lanes of its own', () => due_date: { type: 'date', label: 'Due' }, tags: { type: 'multiselect', label: 'Tags', options: STATUS_OPTIONS }, watchers: { type: 'lookup', label: 'Watchers', reference: 'sys_user', multiple: true }, + meta: { type: 'json', label: 'Meta' }, + address: { type: 'address', label: 'Address' }, + score: { type: 'formula', label: 'Score', expression: 'amount * 2' }, }, }, ]; @@ -197,6 +200,39 @@ describe('#35143 — a board grouped by a field with no lanes of its own', () => for (const line of [findings[0].message, findings[0].hint]) expect(line.length, line).toBeLessThanOrEqual(200); }); + // The grid remedy is offered only where `engine.aggregate` accepts the + // `groupBy` a list view grouping compiles to: a stored, single-valued + // scalar. A multi-value, structured-JSON or `formula` field is refused there + // (`400 INVALID_FIELD`), so its hint offers a grid column instead. + it.each([ + ['amount', true], + ['due_date', true], + ['subject', true], + ['tags', false], + ['watchers', false], + ['meta', false], + ['address', false], + ['score', false], + ])('the hint for a board grouped by `%s` offers a grid grouping: %s', (field, groupable) => { + const [finding] = groupFindings(groupedBy(field as string)); + expect(finding, field as string).toBeDefined(); + if (groupable) { + expect(finding.hint).toContain(`"${field}" as a grid grouping`); + expect(finding.hint).not.toContain('grid column'); + } else { + expect(finding.hint).toContain(`"${field}" as a grid column`); + expect(finding.hint).not.toContain('grid grouping'); + } + expect(finding.hint.length, finding.hint).toBeLessThanOrEqual(200); + }); + + it('`os explain` offers a grid grouping only for a stored, single-valued field', () => { + const fix = explainRule(LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL)!.paragraphs.at(-1)!; + for (const fact of ['grid grouping', 'grid column', 'multi-valued', 'JSON-stored', '`formula`', 'INVALID_FIELD']) { + expect(fix, fact).toContain(fact); + } + }); + it('a kanban block on a non-kanban view, a dotted and an unknown group field are not judged', () => { expect(groupFindings(groupedBy('amount', 'grid'))).toEqual([]); expect(groupFindings(groupedBy('owner.name'))).toEqual([]);