Skip to content

Commit 38a278b

Browse files
fix(spec)!: a dashboard that declares two global filters under one name is refused (#29162)
domain:spec#1 · os-steve · session_01ADHkmPi25XXKx92Xmg4viS <!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9KkaDRBKjsksQZiRDpUqmmU)_ Fixes #29023 Clause-②: no (narrowing) Metadata-protocol: narrowed ## 维护者速读 - 改了什么:仪表盘的 `globalFilters` 里两个筛选器同名(无 `name` 时按 `field` 算)现在在所有解析入口被拒收,保存门答 422 `INVALID_METADATA`,报错落在第二个的 `name` 上并点名两个下标和改法。 - 为什么改:同名时看板只画一个控件,输入的值却去筛另一个定义的字段,卡片数字悄悄错了;卡里复现:值 north 落到 `status` 上,卡片读 0。 - 风险与回滚:收窄,带重复名的仪表盘再保存会被拒;仓内作者写的仪表盘重复数为 0。已存的行照常加载,`_diagnostics` 标 `valid: false`。回滚即还原本 PR。 - 先看:`packages/spec/src/ui/duplicate-global-filter-names.ts`(判定与报错文字)、`packages/spec/src/ui/dashboard.zod.ts`(挂在数组上的 superRefine)、迁移条目 `18.ui-dashboard-global-filter-duplicate-name-refused.ts`。 - 两分钟验证:`pnpm --filter @objectstack/spec exec vitest run src/ui/dashboard-global-filter-duplicate-name.test.ts`,预期 11 项全绿,其中第三项断言报错位置为 `globalFilters[1].name`。 - 需要你做的:无,席位落地(落地前走一次契约复核)。 ## What changes - `DashboardSchema.globalFilters` refuses two or more entries under one filter key. The key is the entry's `name`, or its `field` when it has no `name` (the same derivation as `globalFilterKey` in `system/i18n-resolver.ts`, which the dashboard schema cannot import without a cycle through `ui/component.zod.ts`; a test holds the two equal). One `custom` issue per repeated key, at the SECOND declaration's `name` (or its `field` when that entry is keyed by `field`), naming the key, every index and the remedy: rename all but one (and every widget `filterBindings` key that meant a renamed one) or delete the copy. - The predicate and message live in `packages/spec/src/ui/duplicate-global-filter-names.ts`, the shape of `data/duplicate-action-names.ts`. Neither the predicate nor the refinement is exported, so the public surface does not move. - The refinement is attached to the ARRAY, so `DashboardSchema.shape.globalFilters` carries it into any mirror that spreads the shape (pinned). - Semantic migration entry `ui-dashboard-global-filter-duplicate-name-refused` (step 18), modelled on `ui-dashboard-filter-binding-unknown-refused`: refused with a prescription, no conversion. Which definition keeps the name is the author's call, and an automatic rename would silently unbind every widget that meant the renamed one. - Ledger: `dropped-refinements.baseline.json` gains the five sites the array refinement adds (`ui/Dashboard` `globalFilters` and the four installed-package manifest projections; `droppedRefinementSites` 983 to 988). JSON Schema cannot state unique-by-key, so the rule reaches the runtime and not the file, as the ledger records. - Regenerated: `content/docs/references/ui/dashboard.mdx` (the `globalFilters` describe). - Changeset `.changeset/29023-dashboard-global-filter-duplicate-name.md`, `@objectstack/spec` minor, `adr-0087: registered ui-dashboard-global-filter-duplicate-name-refused`. - No forced test edits: no existing test changed. ## Tests - New `packages/spec/src/ui/dashboard-global-filter-duplicate-name.test.ts`, 11 cases: two CONTROL accepts (distinct names; distinct names over the same field), the card's reproduction refused at `['globalFilters', 1, 'name']` with the message's first sentence pinned, unnamed pair located at `.field`, name colliding with another entry's unnamed field, empty `name` keyed by `field`, three declarations (one issue at the second, every index listed), two repeated keys (one issue each), the rule travelling with `.shape.globalFilters`, parity with `globalFilterKey`, and the predicate leaving malformed input to the schema. - Ablation (spec tests import `src`, so no build leg): deleting the `.superRefine(refuseDuplicateGlobalFilterNames)` line through `scripts/ablation-replace.mjs --delete` (anchor 1 to 0) turns 7 of 11 red, the seven refusal cases; the four that stay green are the two controls and the two predicate-only cases. Restore verified: blob equals HEAD, `git diff HEAD` empty. A first attempt with an empty `--replacement` was refused by the tool (no mutation landed) and is not counted. - Save door, measured with a scratch `saveMetaItem` test over the stub engine of `protocol.draft-publish-verdict.test.ts` (not committed): the card's body answers `422 INVALID_METADATA` with one issue at `globalFilters.1.name` in default, `publish` and `draft` mode, and stores nothing; the distinct-name control saves. A stored row that repeats a key still loads through `getMetaItem`, with `_diagnostics` `valid: false` carrying the refusal. Round 1 (seat): `Metadata-protocol` amended from `semantic` to `narrowed` on the claim and in the changeset (a refused value is `narrowed`); `origin/main` merged at `0d6d4061` through `scripts/pm/os-regen-merge.sh` (no conflict; the branch's regenerated `dashboard.mdx` kept, step 4 rebuilt and `check:generated` reads 14/14 up to date). Readings below are on the merged head `6172f51e` (local, shared box; counts only): <details><summary>Package suites (contract face: spec plus the two consumers the delta names, plus dogfood for the narrowing arm)</summary> - `pnpm --filter @objectstack/spec test`: Test Files 791 passed (791), Tests 23378 passed, 1 todo. Lock VERDICT command-exit 0. - `pnpm --filter @objectstack/lint test` (widget bindings consumer): Test Files 189 passed (189), Tests 7830 passed. VERDICT command-exit 0. - `pnpm --filter @objectstack/metadata-protocol test` (the draft-publish verdict test that names the binding refusal): Test Files 298 passed, 4 skipped (302), Tests 22069 passed, 26 skipped. VERDICT command-exit 0. - `pnpm --filter @objectstack/dogfood test` (closure built first; no shard, no path): Test Files 387 passed, 3 skipped (390), Tests 3204 passed, 28 skipped. VERDICT command-exit 0. - `pnpm --filter @objectstack/spec typecheck` (on `8331c756`, before the merge, which moved no file of this diff): exit 0; the test layer (`tsconfig.test.json`) compiles the new test file with no new debt. </details> ## Acceptance notes - Blast radius, measured on this tree: 95 `globalFilters` arrays across 136 files (examples, packages, skills, docs, ui), 14 with two or more entries, zero repeat a key in authored metadata. One hit is a ui renderer unit test (`ui/packages/core/src/utils/__tests__/dashboard-filters.test.ts`, "lets duplicate names win last") that feeds `resolveDashboardFilterDefs` directly and never parses through the spec; it stays green and is outside this card (`ui/**` not touched). HotCRM: NOT MEASURED (outside this session's repository scope). - `packages/lint` `validate-widget-bindings.ts` keys filters by name with later-wins semantics; once the schema refuses duplicates it needs no change, and no gate asked for one. - The reserved name `dateRange`: a `globalFilters` entry named `dateRange` on a dashboard that also declares `dateRange` is not refused by this rule (the lint resolver lets the later definition win). Out of scope here; reported to the seat. - `os migrate meta --stored` does not list stored dashboards this refuses: the v18 stored-row listing (`conversions/refused-shapes.ts`) carries a detector for the action-name sibling but none was added here (outside the claim's file surface). Reported to the seat as an option. ## Verification <details><summary>Derived gates: 106 derived, 106 run, 0 NOT-MEASURED, 0 UNRUN</summary> - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` on `6172f51e` (7 paths vs merge base `0d6d4061`): 106 commands, every one exit 0 on the first pass. - (On the pre-merge head `8331c756`, three first exited 3 on prerequisites, a shallow clone and missing built output, and were re-run green; this round built those packages up front.) - `dispatch-gates --ran`: 106 derived famil(ies) accounted for, 106 run, 0 NOT-MEASURED. - Changeset level axis, offline (`check-changeset-no-major.mjs --event` with this body's two lines, base `0d6d4061`): exit 0, `Clause-②: no (narrowing)` with `Metadata-protocol: narrowed` accepted. - CI families: per the PR's check-runs. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmNmHGTQ6fDSRm1srQr)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 27c1c55 commit 38a278b

7 files changed

Lines changed: 340 additions & 4 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@objectstack/spec': minor
3+
---
4+
5+
fix(spec)!: a dashboard that declares two global filters under one name is refused at every door that parses it
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: narrowed
9+
Skills-docs: none — `skills/objectstack-ui/rules/dashboards.md` teaches a filter's `name` as "the stable key `filterBindings` references", defaulting to `field`, and no file under `skills/**` shows two filters under one key; every taught use stays true.
10+
11+
**BREAKING** for a dashboard whose `globalFilters` repeats a filter key.
12+
13+
- **Before.** `DashboardSchema` typed `globalFilters` as a plain array, so two entries under one key parsed and the dashboard save door (`PUT /api/v1/meta/dashboard/X`) answered 200. A filter's key is its `name`, or its `field` when it has no `name`: the key the filter bar draws its control under and the key a widget's `filterBindings` names. The board drew one control, and the value typed into it filtered the field of the other definition, so a tile read a number the author never asked for.
14+
- **Now.** `DashboardSchema.globalFilters` refuses the pair, so every door that parses a dashboard refuses it: `defineStack`, `os validate`, `os build` and the dashboard save door (422 `INVALID_METADATA`). One issue per repeated key, at the second declaration's `name` (or its `field`, when that entry has no `name`), naming the key and every index: `This dashboard declares the global filter 'X' twice in globalFilters: globalFilters[i] and globalFilters[j]. …`. The rule sits on the array, so a mirror that spreads `DashboardSchema.shape` keeps it.
15+
- **Not changed.** Distinct names, two filters with distinct names over the same `field`, and the same name on two different dashboards.
16+
- **Fix.** Rename all but one of the colliding entries (and every widget `filterBindings` key that means a renamed one), or delete the copy.
17+
- **Stored rows.** A stored dashboard that repeats a key still loads, with `_diagnostics` reading `valid: false` and carrying the refusal, and its next save (draft or publish) is refused until it is fixed. Nothing rewrites it: which definition keeps the name only the author knows. The upgrade guide entry is `ui-dashboard-global-filter-duplicate-name-refused`.
18+
19+
<!-- adr-0087: registered ui-dashboard-global-filter-duplicate-name-refused -->

‎content/docs/references/ui/dashboard.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const result = DashboardSchema.parse(data);
3838
| **refreshIntervalSeconds** | `number` | optional | Auto-refresh interval in seconds |
3939
| **refreshInterval** | `never` | optional | [REMOVED] `dashboard.refreshInterval` was renamed to `refreshIntervalSeconds` in @objectstack/spec 17 — the unit of a duration-shaped number lives in the key name, not only in the describe prose. Rename the key to `refreshIntervalSeconds`; the value (seconds) is unchanged. Run `os migrate meta --from 17` to list the mechanical edits for existing sources; `--write` applies the ones it can prove, and you apply the rest by hand. |
4040
| **dateRange** | `{ field?: string; defaultRange?: Enum<'today' \| 'yesterday' \| 'this_week' \| 'last_week' \| 'this_month' \| 'last_month' \| …>; allowCustomRange?: boolean }` | optional | Global dashboard date range filter configuration |
41-
| **globalFilters** | `{ name?: string; field: string; object?: string; label?: string \| Record<string, string>; … }[]` | optional | Global filters that apply to all widgets in the dashboard |
41+
| **globalFilters** | `{ name?: string; field: string; object?: string; label?: string \| Record<string, string>; … }[]` | optional | Global filters that apply to all widgets in the dashboard. Each filter name (its field when it has no name) is declared once |
4242
| **requiredPermissions** | `string[]` | optional | [ADR-0066] Capabilities a user must ALL hold for this list view or dashboard to be served to them — names that permission sets grant through `systemPermissions`, the same list as `requiredPermissions` on an app, a navigation item or an action. Absent or empty: no audience gate. There is no any-of form: to serve one item to several audiences (legal OR admin), declare one capability and grant it to each of their permission sets. The server enforces it: a user who does not hold them all is not listed the item and is refused it by name; a view container's list views are pruned the same way, and so are an object's own list views for a user who may not edit the object. |
4343
| **protection** | `{ lock: Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>; reason: string; docsUrl?: string }` | optional | Package author protection block — lock policy for this dashboard. |
4444
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |

‎packages/spec/dropped-refinements.baseline.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"measured": {
44
"zod": "4.4.3",
55
"publishedSchemasWithDroppedRefinements": 248,
6-
"droppedRefinementSites": 983,
6+
"droppedRefinementSites": 988,
77
"refinementSitesThatDidProject": 369,
88
"refinementSitesWithNoJsonFormToCompare": 0
99
},
@@ -81,6 +81,7 @@
8181
"manifest.assignmentRules.element.entries.element",
8282
"manifest.assignmentRules.element.entries.element.assignTo",
8383
"manifest.connectors.element.out",
84+
"manifest.dashboards.element.globalFilters",
8485
"manifest.dashboards.element.globalFilters.element",
8586
"manifest.dashboards.element.header.actions.element",
8687
"manifest.dashboards.element.widgets.element",
@@ -227,6 +228,7 @@
227228
"data.options[1].manifest.assignmentRules.element.entries.element",
228229
"data.options[1].manifest.assignmentRules.element.entries.element.assignTo",
229230
"data.options[1].manifest.connectors.element.out",
231+
"data.options[1].manifest.dashboards.element.globalFilters",
230232
"data.options[1].manifest.dashboards.element.globalFilters.element",
231233
"data.options[1].manifest.dashboards.element.header.actions.element",
232234
"data.options[1].manifest.dashboards.element.widgets.element",
@@ -365,6 +367,7 @@
365367
"options[1].manifest.assignmentRules.element.entries.element",
366368
"options[1].manifest.assignmentRules.element.entries.element.assignTo",
367369
"options[1].manifest.connectors.element.out",
370+
"options[1].manifest.dashboards.element.globalFilters",
368371
"options[1].manifest.dashboards.element.globalFilters.element",
369372
"options[1].manifest.dashboards.element.header.actions.element",
370373
"options[1].manifest.dashboards.element.widgets.element",
@@ -443,6 +446,7 @@
443446
"data.packages.element.options[1].manifest.assignmentRules.element.entries.element",
444447
"data.packages.element.options[1].manifest.assignmentRules.element.entries.element.assignTo",
445448
"data.packages.element.options[1].manifest.connectors.element.out",
449+
"data.packages.element.options[1].manifest.dashboards.element.globalFilters",
446450
"data.packages.element.options[1].manifest.dashboards.element.globalFilters.element",
447451
"data.packages.element.options[1].manifest.dashboards.element.header.actions.element",
448452
"data.packages.element.options[1].manifest.dashboards.element.widgets.element",
@@ -1588,6 +1592,7 @@
15881592
},
15891593
"ui/Dashboard": {
15901594
"sites": [
1595+
"globalFilters",
15911596
"globalFilters.element",
15921597
"header.actions.element",
15931598
"widgets.element",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import type { SemanticMigration } from '../../types.js';
4+
5+
// #29023. An accept-set narrowing of the dashboard schema with no conversion:
6+
// two globalFilters entries under one key (name, else field) are refused by the
7+
// dashboard parse. Which definition keeps the name, and which widgets bind the
8+
// renamed one, only the author knows. Same class as
9+
// ui-dashboard-filter-binding-unknown-refused.
10+
export const entry: SemanticMigration = {
11+
id: 'ui-dashboard-global-filter-duplicate-name-refused',
12+
surface: 'dashboard.globalFilters — two or more entries under the same filter key (the entry\'s '
13+
+ '`name`, or its `field` when it has no `name`), e.g. globalFilters: [{ name: \'r\', field: '
14+
+ '\'region\' }, { name: \'r\', field: \'status\' }]',
15+
replacement: 'One entry per filter key on each dashboard. Rename all but one of the colliding entries '
16+
+ '(and every widget `filterBindings` key that means a renamed one), or delete the copy. Two filters '
17+
+ 'over the same `field` stay legal when their names differ.',
18+
reason:
19+
'A global filter\'s key is the one the filter bar draws its control under, the key a widget\'s '
20+
+ '`filterBindings` names and the `page.<name>` variable its value is published as. The schema typed '
21+
+ '`globalFilters` as a plain array, so two entries under one key parsed and the dashboard save door '
22+
+ 'answered 200. The board then drew ONE control, and the value typed into it filtered the field of '
23+
+ 'the other definition: the widget query carried the second entry\'s field and the tile read a number '
24+
+ 'the author never asked for, with nothing on the page to say so. `DashboardSchema` now refuses the '
25+
+ 'pair at every door that parses a dashboard: `defineStack`, `os validate`, `os build` and the '
26+
+ 'dashboard save door (`422 INVALID_METADATA`), with one issue at the second declaration\'s `name` '
27+
+ '(or `field`) naming the key and every index. Not convertible: which definition keeps the name, and '
28+
+ 'whether the copy should go, is a fact only the author holds, and renaming on their behalf would '
29+
+ 'silently unbind every widget that meant the renamed one.',
30+
acceptanceCriteria:
31+
'Each dashboard parses without a `custom` issue at `globalFilters[i].name` (or `.field`) of the form '
32+
+ '"This dashboard declares the global filter X twice in globalFilters: globalFilters[i] and '
33+
+ 'globalFilters[j]", and saves. After renaming, check that every widget `filterBindings` key names the '
34+
+ 'filter it meant, and that `os validate` reports no `dashboard-filter-binding-unknown` finding. A '
35+
+ 'stored `sys_metadata` dashboard that still repeats a key still loads, with `_diagnostics` reading '
36+
+ '`valid: false` and carrying this refusal, and its next save (draft or publish) is refused; nothing '
37+
+ 'rewrites it. Measured before the change: no authored dashboard in this repository\'s examples, '
38+
+ 'platform objects, docs or skills repeats a filter key.',
39+
};
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #29023 — a dashboard declares each global filter key (its `name`, else its
5+
* `field`) once.
6+
*
7+
* Measured on `main` @ `12aad143` before the rule: `DashboardSchema` accepted
8+
* `globalFilters: [{ name: 'r', field: 'region' }, { name: 'r', field: 'status' }]`,
9+
* the save door answered 200, and the board drew ONE control whose value
10+
* filtered `status`, the second definition's field. The refusal is located at
11+
* the second declaration's key and names every index.
12+
*/
13+
import { describe, expect, it } from 'vitest';
14+
import { DashboardSchema } from './dashboard.zod';
15+
import { duplicateGlobalFilterNames } from './duplicate-global-filter-names';
16+
import { globalFilterKey } from '../system/i18n-resolver';
17+
18+
const board = (globalFilters: unknown[]) => ({
19+
name: 'qa_probe_dfn',
20+
label: 'QA probe',
21+
widgets: [],
22+
globalFilters,
23+
});
24+
25+
const customIssues = (input: unknown) => {
26+
const result = DashboardSchema.safeParse(input);
27+
if (result.success) return [];
28+
return result.error.issues.filter((i) => i.code === 'custom');
29+
};
30+
31+
describe('#29023 DashboardSchema refuses two globalFilters under one name', () => {
32+
it('CONTROL: two filters with distinct names parse', () => {
33+
const result = DashboardSchema.safeParse(board([
34+
{ name: 'r', field: 'region', type: 'text' },
35+
{ name: 's', field: 'status', type: 'text' },
36+
]));
37+
expect(result.success).toBe(true);
38+
});
39+
40+
it('CONTROL: two distinct names over the SAME field parse (the key is the name, not the field)', () => {
41+
const result = DashboardSchema.safeParse(board([
42+
{ name: 'region_a', field: 'region', type: 'text' },
43+
{ name: 'region_b', field: 'region', type: 'select', options: [{ value: 'north', label: 'North' }] },
44+
]));
45+
expect(result.success).toBe(true);
46+
});
47+
48+
it('the card reproduction is refused at globalFilters[1].name, naming both indices, the name and the fix', () => {
49+
const result = DashboardSchema.safeParse(board([
50+
{ name: 'r', field: 'region', type: 'text' },
51+
{ name: 'r', field: 'status', type: 'text' },
52+
]));
53+
expect(result.success).toBe(false);
54+
const issues = result.error!.issues;
55+
expect(issues).toHaveLength(1);
56+
expect(issues[0]).toMatchObject({ code: 'custom', path: ['globalFilters', 1, 'name'] });
57+
expect(issues[0]!.message.startsWith(
58+
"This dashboard declares the global filter 'r' twice in globalFilters: globalFilters[0] and globalFilters[1].",
59+
)).toBe(true);
60+
expect(issues[0]!.message).toMatch(/Rename all but one of them .* or delete the copy\.$/);
61+
// Runtime strings carry no tracker number (check:doc-authoring).
62+
expect(issues[0]!.message).not.toMatch(/#\d{3,}/);
63+
});
64+
65+
it('two UNNAMED filters on one field collide on the field, located at globalFilters[1].field', () => {
66+
const issues = customIssues(board([
67+
{ field: 'region', type: 'text' },
68+
{ field: 'region', type: 'select', options: [{ value: 'north', label: 'North' }] },
69+
]));
70+
expect(issues).toHaveLength(1);
71+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] });
72+
expect(issues[0]!.message).toContain("the global filter 'region' twice");
73+
expect(issues[0]!.message).toContain('A filter with no `name` is keyed by its `field`.');
74+
});
75+
76+
it('a name colliding with another entry\'s field (no name) is the same key and is refused', () => {
77+
const issues = customIssues(board([
78+
{ field: 'region', type: 'text' },
79+
{ name: 'region', field: 'sales_region', type: 'text' },
80+
]));
81+
expect(issues).toHaveLength(1);
82+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'name'] });
83+
});
84+
85+
it('an empty `name` keys by `field`, as `globalFilterKey` does', () => {
86+
const issues = customIssues(board([
87+
{ name: '', field: 'region', type: 'text' },
88+
{ field: 'region', type: 'text' },
89+
]));
90+
expect(issues).toHaveLength(1);
91+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] });
92+
});
93+
94+
it('three declarations of one name answer ONE issue at the second, listing every index', () => {
95+
const issues = customIssues(board([
96+
{ name: 'r', field: 'region' },
97+
{ name: 'other', field: 'owner' },
98+
{ name: 'r', field: 'status' },
99+
{ name: 'r', field: 'stage' },
100+
]));
101+
expect(issues).toHaveLength(1);
102+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 2, 'name'] });
103+
expect(issues[0]!.message.startsWith(
104+
"This dashboard declares the global filter 'r' 3 times in globalFilters: "
105+
+ 'globalFilters[0], globalFilters[2] and globalFilters[3].',
106+
)).toBe(true);
107+
});
108+
109+
it('two repeated names answer one issue each', () => {
110+
const issues = customIssues(board([
111+
{ name: 'a', field: 'region' },
112+
{ name: 'b', field: 'owner' },
113+
{ name: 'a', field: 'status' },
114+
{ name: 'b', field: 'stage' },
115+
]));
116+
expect(issues.map((i) => i.path)).toEqual([['globalFilters', 2, 'name'], ['globalFilters', 3, 'name']]);
117+
});
118+
119+
it('the rule travels with `DashboardSchema.shape.globalFilters` (a mirror spreading the shape keeps it)', () => {
120+
const slot = (DashboardSchema as unknown as { shape: Record<string, { safeParse(v: unknown): { success: boolean } }> })
121+
.shape.globalFilters!;
122+
expect(slot.safeParse([{ name: 'r', field: 'region' }, { name: 'r', field: 'status' }]).success).toBe(false);
123+
expect(slot.safeParse([{ name: 'r', field: 'region' }, { name: 's', field: 'status' }]).success).toBe(true);
124+
expect(slot.safeParse(undefined).success).toBe(true);
125+
});
126+
});
127+
128+
describe('#29023 duplicateGlobalFilterNames keys a filter the way globalFilterKey does', () => {
129+
const filters = [
130+
{ name: 'dept', field: 'department' },
131+
{ field: 'created_at' },
132+
{ name: '', field: 'owner_id' },
133+
{ name: 'stage' , field: 'stage' },
134+
{ label: 'Unkeyed' },
135+
];
136+
137+
it('every keyed filter resolves to globalFilterKey(filter), and an unkeyed one is passed over', () => {
138+
// Doubling each filter makes every keyed one a duplicate, so the list reads back the predicate's keys.
139+
const keys = duplicateGlobalFilterNames([...filters, ...filters]).map((d) => d.name);
140+
expect(keys).toEqual(filters.map((f) => globalFilterKey(f)).filter((k): k is string => k !== undefined));
141+
});
142+
143+
it('a non-array, a non-object entry and an entry with neither key are left to the schema', () => {
144+
expect(duplicateGlobalFilterNames(undefined)).toEqual([]);
145+
expect(duplicateGlobalFilterNames({ name: 'r' })).toEqual([]);
146+
expect(duplicateGlobalFilterNames([null, 'r', 7, { label: 'x' }, { label: 'y' }])).toEqual([]);
147+
});
148+
});

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { DATE_RANGE_PRESETS } from '../data/date-range-presets';
1414
import { ChartTypeSchema, ChartConfigSchema, type ChartType } from './chart.zod';
1515
import { ActionType } from './action.zod';
1616
import { navigationTargetRefusal } from './navigation-target';
17+
import { duplicateGlobalFilterNames } from './duplicate-global-filter-names';
1718
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
1819
// `AriaPropsSchema` is no longer imported here: `widgets[].aria` was retired
1920
// (#5010). The shape itself is NOT removed — it stays live on `page.aria` /
@@ -1668,6 +1669,25 @@ export const GlobalFilterSchema = lazySchema(() => strictObject({
16681669
// and it must be this binding, not a copy.
16691670
.superRefine(checkGlobalFilterDateDefaultValue));
16701671

1672+
/**
1673+
* A dashboard's `globalFilters` declares each filter key once (#29023). One
1674+
* located issue per repeated key, at the SECOND declaration's `name` (or its
1675+
* `field`, when that entry is keyed by `field`), naming the key and every
1676+
* index. Before this, the dashboard save door saved two filters under one
1677+
* name: the board drew one control, and its value filtered the other
1678+
* definition's field, so a tile read a number the author never asked for.
1679+
*
1680+
* Attached to the array, not to the dashboard object, so the rule travels with
1681+
* `DashboardSchema.shape.globalFilters` into any mirror that spreads the
1682+
* shape. Predicate and message: `duplicate-global-filter-names.ts`
1683+
* ({@link duplicateGlobalFilterNames}). Not exported: the schema is the door.
1684+
*/
1685+
function refuseDuplicateGlobalFilterNames(globalFilters: readonly unknown[], ctx: z.RefinementCtx): void {
1686+
for (const duplicate of duplicateGlobalFilterNames(globalFilters)) {
1687+
ctx.addIssue({ code: 'custom', path: [duplicate.indexes[1]!, duplicate.secondKey], message: duplicate.message });
1688+
}
1689+
}
1690+
16711691
/**
16721692
* Dashboard Schema
16731693
* Represents a page containing multiple visualizations.
@@ -1768,8 +1788,17 @@ export const DashboardSchema = lazySchema(() => strictObject({
17681788
allowCustomRange: z.boolean().default(true).describe('Allow users to pick a custom date range'),
17691789
}).optional().describe('Global dashboard date range filter configuration'),
17701790

1771-
/** Global Filters */
1772-
globalFilters: z.array(GlobalFilterSchema).optional().describe('Global filters that apply to all widgets in the dashboard'),
1791+
/**
1792+
* Global Filters. Each filter's key (its `name`, else its `field`) is
1793+
* declared once per dashboard: the key is what the filter bar draws a
1794+
* control under and what a widget's `filterBindings` names, so two entries
1795+
* under one key are two definitions behind one control. Refused by
1796+
* {@link refuseDuplicateGlobalFilterNames} (#29023).
1797+
*/
1798+
globalFilters: z.array(GlobalFilterSchema)
1799+
.superRefine(refuseDuplicateGlobalFilterNames)
1800+
.optional()
1801+
.describe('Global filters that apply to all widgets in the dashboard. Each filter name (its field when it has no name) is declared once'),
17731802

17741803
/**
17751804
* [#22611] The audience gate — the platform's one capability key, with the

0 commit comments

Comments
 (0)