Skip to content

Commit 26cd84c

Browse files
committed
fix(spec)!: refuse a globalFilters entry keyed dateRange on a dashboard that declares dateRange
The built-in date range is declared under the reserved key `dateRange`; an entry keyed `dateRange` beside it parsed and replaced the date range's control and binding in the console and lint resolvers. Claude-Session: https://claude.ai/code/session_01ADHkmNvZN4TxxGwnpRp9yG Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5a2bdf9 commit 26cd84c

3 files changed

Lines changed: 241 additions & 10 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #29213 — on a dashboard that declares the built-in `dateRange`, a
5+
* `globalFilters` entry keyed `dateRange` (its `name`, else its `field`) is
6+
* refused.
7+
*
8+
* Measured on `main` @ `5a2bdf9c` before the rule: `DashboardSchema` accepted
9+
* `{ dateRange: { field: 'close_date' }, globalFilters: [{ name: 'dateRange', field: 'region' }] }`,
10+
* and the console's `resolveDashboardFilterDefs` (like lint's
11+
* `dashboardFilterDefs`) built the built-in date range first and let the
12+
* entry replace it, so the board drew a `region` control where the date range
13+
* belonged and `filterBindings.dateRange` bound `region`. The refusal is
14+
* located at the entry's key.
15+
*/
16+
import { describe, expect, it } from 'vitest';
17+
import { DashboardSchema } from './dashboard.zod';
18+
import { reservedDateRangeGlobalFilters } from './duplicate-global-filter-names';
19+
20+
const board = (extra: Record<string, unknown>) => ({
21+
name: 'qa_probe_drg',
22+
label: 'QA probe',
23+
widgets: [],
24+
...extra,
25+
});
26+
27+
const customIssues = (input: unknown) => {
28+
const result = DashboardSchema.safeParse(input);
29+
if (result.success) return [];
30+
return result.error.issues.filter((i) => i.code === 'custom');
31+
};
32+
33+
describe('#29213 DashboardSchema refuses a globalFilters entry keyed dateRange beside a declared dateRange', () => {
34+
it('CONTROL: a declared dateRange beside a differently named filter parses', () => {
35+
const result = DashboardSchema.safeParse(board({
36+
dateRange: { field: 'close_date' },
37+
globalFilters: [{ name: 'region', field: 'region', type: 'text' }],
38+
}));
39+
expect(result.success).toBe(true);
40+
});
41+
42+
it('CONTROL: with no dateRange declared, an entry named dateRange is an ordinary filter and parses', () => {
43+
const result = DashboardSchema.safeParse(board({
44+
globalFilters: [{ name: 'dateRange', field: 'close_date', type: 'date' }],
45+
}));
46+
expect(result.success).toBe(true);
47+
});
48+
49+
it('the card reproduction is refused at globalFilters[0].name, naming the reserved word and the built-in date range', () => {
50+
const result = DashboardSchema.safeParse(board({
51+
dateRange: { field: 'close_date' },
52+
globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }],
53+
}));
54+
expect(result.success).toBe(false);
55+
const issues = result.error!.issues;
56+
expect(issues).toHaveLength(1);
57+
expect(issues[0]).toMatchObject({ code: 'custom', path: ['globalFilters', 0, 'name'] });
58+
expect(issues[0]!.message.startsWith(
59+
"globalFilters[0] is keyed 'dateRange', the reserved name of the built-in date range this dashboard declares in `dateRange`.",
60+
)).toBe(true);
61+
expect(issues[0]!.message).toMatch(/Give this filter another `name` .* or delete it and keep the built-in `dateRange`\.$/);
62+
// Runtime strings carry no tracker number (check:doc-authoring).
63+
expect(issues[0]!.message).not.toMatch(/#\d{3,}/);
64+
});
65+
66+
it('an empty dateRange object is a declared dateRange (the shape both resolvers test)', () => {
67+
const issues = customIssues(board({
68+
dateRange: {},
69+
globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }],
70+
}));
71+
expect(issues).toHaveLength(1);
72+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 0, 'name'] });
73+
});
74+
75+
it('an UNNAMED entry whose field is dateRange is keyed by the field, located at .field', () => {
76+
const issues = customIssues(board({
77+
dateRange: {},
78+
globalFilters: [{ name: 'region', field: 'region', type: 'text' }, { field: 'dateRange', type: 'text' }],
79+
}));
80+
expect(issues).toHaveLength(1);
81+
expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] });
82+
expect(issues[0]!.message).toContain('(a filter with no `name` is keyed by its `field`)');
83+
});
84+
85+
it('CONTROL: a named entry over a field called dateRange is keyed by its name and parses', () => {
86+
const result = DashboardSchema.safeParse(board({
87+
dateRange: {},
88+
globalFilters: [{ name: 'period', field: 'dateRange', type: 'text' }],
89+
}));
90+
expect(result.success).toBe(true);
91+
});
92+
93+
it('two entries keyed dateRange answer one reserved-name issue each, beside the duplicate-key issue', () => {
94+
const issues = customIssues(board({
95+
dateRange: {},
96+
globalFilters: [{ name: 'dateRange', field: 'region' }, { name: 'dateRange', field: 'status' }],
97+
}));
98+
const reserved = issues.filter((i) => i.message.includes('reserved name of the built-in date range'));
99+
expect(reserved.map((i) => i.path)).toEqual([['globalFilters', 0, 'name'], ['globalFilters', 1, 'name']]);
100+
expect(issues.filter((i) => i.message.startsWith('This dashboard declares the global filter')).map((i) => i.path))
101+
.toEqual([['globalFilters', 1, 'name']]);
102+
});
103+
104+
it('the refusal still answers when another key of the dashboard fails too', () => {
105+
const result = DashboardSchema.safeParse(board({
106+
columns: 99,
107+
dateRange: {},
108+
globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }],
109+
}));
110+
expect(result.success).toBe(false);
111+
const paths = result.error!.issues.map((i) => i.path.join('.'));
112+
expect(paths).toContain('columns');
113+
expect(paths).toContain('globalFilters.0.name');
114+
});
115+
});
116+
117+
describe('#29213 reservedDateRangeGlobalFilters reads a stored body', () => {
118+
it('answers nothing without a declared dateRange object', () => {
119+
const filters = [{ name: 'dateRange', field: 'region' }];
120+
expect(reservedDateRangeGlobalFilters(undefined, filters)).toEqual([]);
121+
expect(reservedDateRangeGlobalFilters(null, filters)).toEqual([]);
122+
expect(reservedDateRangeGlobalFilters('this_month', filters)).toEqual([]);
123+
});
124+
125+
it('a non-array, a non-object entry and an entry with neither key are left to the schema', () => {
126+
expect(reservedDateRangeGlobalFilters({}, undefined)).toEqual([]);
127+
expect(reservedDateRangeGlobalFilters({}, { name: 'dateRange' })).toEqual([]);
128+
expect(reservedDateRangeGlobalFilters({}, [null, 'dateRange', 7, { label: 'dateRange' }])).toEqual([]);
129+
});
130+
131+
it('an empty name falls back to the field, as the duplicate-key predicate does', () => {
132+
expect(reservedDateRangeGlobalFilters({}, [{ name: '', field: 'dateRange' }]).map((e) => [e.index, e.key]))
133+
.toEqual([[0, 'field']]);
134+
});
135+
});

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +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';
17+
import { duplicateGlobalFilterNames, reservedDateRangeGlobalFilters } from './duplicate-global-filter-names';
1818
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
1919
// `AriaPropsSchema` is no longer imported here: `widgets[].aria` was retired
2020
// (#5010). The shape itself is NOT removed — it stays live on `page.aria` /
@@ -1396,7 +1396,9 @@ export const DashboardWidgetSchema = lazySchema(() => strictObject({
13961396
*
13971397
* Every key must name a filter the dashboard declares — a `globalFilters`
13981398
* entry's `name` (its `field` when unnamed), or `dateRange` when the
1399-
* dashboard declares `dateRange`. The runtime reads a binding by the declared
1399+
* dashboard declares `dateRange` (a `globalFilters` entry keyed `dateRange`
1400+
* beside it is refused, so that key always means the built-in date range
1401+
* there). The runtime reads a binding by the declared
14001402
* filter's name, so any other key is never read; the lint rule
14011403
* `dashboard-filter-binding-unknown` (severity error) refuses it at
14021404
* `os validate` / `os build` and at the metadata publish door.
@@ -1588,9 +1590,12 @@ export const GlobalFilterSchema = lazySchema(() => strictObject({
15881590
* which the filter's value is published (readable in widget expressions as
15891591
* `page.<name>`) and the key widgets reference in `filterBindings`.
15901592
* Defaults to `field`. The name `"dateRange"` is reserved for the built-in
1591-
* dashboard date range.
1593+
* dashboard date range: on a dashboard that declares `dateRange`, an entry
1594+
* keyed `dateRange` (by this name, or by `field` when unnamed) is refused,
1595+
* because it would replace the date range's control and binding. On a
1596+
* dashboard with no `dateRange` the key is free.
15921597
*/
1593-
name: z.string().optional().describe('Stable filter name (variable key); defaults to field').meta({ title: 'Name' }),
1598+
name: z.string().optional().describe('Stable filter name (variable key); defaults to field. Refused as dateRange when the dashboard declares dateRange (the built-in date range\'s reserved name)').meta({ title: 'Name' }),
15941599

15951600
/**
15961601
* Field name to filter on — at the authoring layer it resolves against the
@@ -1688,6 +1693,32 @@ function refuseDuplicateGlobalFilterNames(globalFilters: readonly unknown[], ctx
16881693
}
16891694
}
16901695

1696+
/**
1697+
* A dashboard that declares the built-in `dateRange` declares a filter under
1698+
* the reserved key `dateRange`, so a `globalFilters` entry keyed `dateRange`
1699+
* (its `name`, else its `field`) is refused, located at that entry's key.
1700+
* Before this, the pair parsed and the dashboard save door stored it: the
1701+
* console and lint resolvers let the later definition win, so the board drew
1702+
* the entry's control in place of the declared date range and every widget's
1703+
* `filterBindings.dateRange` was filtered by the entry. On a dashboard with no
1704+
* `dateRange` the key is free and the entry is an ordinary filter.
1705+
*
1706+
* Attached to the dashboard object, not to the `globalFilters` array, because
1707+
* the verdict reads the sibling `dateRange`: a mirror that spreads
1708+
* `DashboardSchema.shape.globalFilters` alone keeps the duplicate-key rule
1709+
* above and not this one. Predicate and message:
1710+
* `duplicate-global-filter-names.ts` ({@link reservedDateRangeGlobalFilters}).
1711+
* Not exported: the schema is the door.
1712+
*/
1713+
function refuseReservedDateRangeGlobalFilters(
1714+
dashboard: { dateRange?: unknown; globalFilters?: unknown },
1715+
ctx: z.RefinementCtx,
1716+
): void {
1717+
for (const entry of reservedDateRangeGlobalFilters(dashboard.dateRange, dashboard.globalFilters)) {
1718+
ctx.addIssue({ code: 'custom', path: ['globalFilters', entry.index, entry.key], message: entry.message });
1719+
}
1720+
}
1721+
16911722
/**
16921723
* Dashboard Schema
16931724
* Represents a page containing multiple visualizations.
@@ -1793,12 +1824,15 @@ export const DashboardSchema = lazySchema(() => strictObject({
17931824
* declared once per dashboard: the key is what the filter bar draws a
17941825
* control under and what a widget's `filterBindings` names, so two entries
17951826
* under one key are two definitions behind one control. Refused by
1796-
* {@link refuseDuplicateGlobalFilterNames} (#29023).
1827+
* {@link refuseDuplicateGlobalFilterNames} (#29023). The built-in
1828+
* `dateRange` is declared under the key `dateRange`, so an entry keyed
1829+
* `dateRange` beside it is refused too, by the dashboard-level
1830+
* {@link refuseReservedDateRangeGlobalFilters} (#29213).
17971831
*/
17981832
globalFilters: z.array(GlobalFilterSchema)
17991833
.superRefine(refuseDuplicateGlobalFilterNames)
18001834
.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'),
1835+
.describe('Global filters that apply to all widgets in the dashboard. Each filter name (its field when it has no name) is declared once, and is not dateRange when the dashboard declares dateRange'),
18021836

18031837
/**
18041838
* [#22611] The audience gate — the platform's one capability key, with the
@@ -1830,7 +1864,7 @@ export const DashboardSchema = lazySchema(() => strictObject({
18301864
// ADR-0146 D2 — the override declaration (authored; travels with the item).
18311865
...OverrideDeclarationFields,
18321866

1833-
}));
1867+
}).superRefine(refuseReservedDateRangeGlobalFilters));
18341868

18351869
export type Dashboard = z.input<typeof DashboardSchema>;
18361870
/** Post-parse shape of {@link Dashboard} — defaults applied, transforms run (ADR-0122). */

‎packages/spec/src/ui/duplicate-global-filter-names.ts‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
* so every door that parses a dashboard refuses the pair: `defineStack`,
1919
* `os validate`, `os build` and the dashboard save door.
2020
*
21-
* Scope is ONE dashboard's own `globalFilters` array. The reserved name
22-
* `dateRange` (the built-in date range) is a different question and is not
23-
* judged here.
21+
* Scope of {@link duplicateGlobalFilterNames} is ONE dashboard's own
22+
* `globalFilters` array. The built-in date range is the other declaration a
23+
* key can collide with: a dashboard that declares `dateRange` declares a
24+
* filter under the reserved key `dateRange`, ahead of every `globalFilters`
25+
* entry, and {@link reservedDateRangeGlobalFilters} answers which entries
26+
* reuse that key.
2427
*/
2528

2629
/** One filter key that a dashboard's `globalFilters` array declares more than once. */
@@ -94,3 +97,62 @@ export function duplicateGlobalFilterNames(globalFilters: unknown): DuplicateGlo
9497
}
9598
return out;
9699
}
100+
101+
/** The key the built-in date range (`dashboard.dateRange`) is declared under. */
102+
export const RESERVED_DATE_RANGE_FILTER_NAME = 'dateRange';
103+
104+
/** A `globalFilters` entry keyed by the built-in date range's reserved name. */
105+
export interface ReservedDateRangeGlobalFilter {
106+
/** The entry's index in `globalFilters`. */
107+
readonly index: number;
108+
/** `name` when the entry authors one, `field` when the key falls back to it. The refusal is located there. */
109+
readonly key: 'name' | 'field';
110+
/** The refusal's message, naming the reserved word, the built-in date range and the fix. */
111+
readonly message: string;
112+
}
113+
114+
/**
115+
* Every `globalFilters` entry keyed `dateRange` (by its `name`, else its
116+
* `field`) on a dashboard that declares the built-in `dateRange`, ascending.
117+
*
118+
* The built-in date range is a filter the board declares under the reserved
119+
* key `dateRange`, so such an entry is a second definition behind that key:
120+
* the console's `resolveDashboardFilterDefs` and lint's `dashboardFilterDefs`
121+
* both build the built-in first and let the later definition win, so the
122+
* filter bar draws the entry's control in place of the date range, and every
123+
* widget's `filterBindings.dateRange` binds the entry. The declared
124+
* `dateRange` is never drawn, while `@objectstack/verify`'s window proof still
125+
* proves it.
126+
*
127+
* On a dashboard that does NOT declare `dateRange` the key is free and the
128+
* entry is an ordinary filter (the board draws it, `filterBindings.dateRange`
129+
* binds it), so nothing is reported. `dateRange` is read as declared when it is
130+
* any non-null object, the shape both resolvers test. Read on a parsed
131+
* dashboard or a stored body alike.
132+
*/
133+
export function reservedDateRangeGlobalFilters(
134+
dateRange: unknown,
135+
globalFilters: unknown,
136+
): ReservedDateRangeGlobalFilter[] {
137+
if (dateRange === null || typeof dateRange !== 'object') return [];
138+
if (!Array.isArray(globalFilters)) return [];
139+
const out: ReservedDateRangeGlobalFilter[] = [];
140+
globalFilters.forEach((filter, index) => {
141+
const resolved = filterKeyOf(filter);
142+
if (!resolved || resolved.name !== RESERVED_DATE_RANGE_FILTER_NAME) return;
143+
out.push({
144+
index,
145+
key: resolved.key,
146+
message:
147+
`globalFilters[${index}] is keyed '${RESERVED_DATE_RANGE_FILTER_NAME}'`
148+
+ (resolved.key === 'field' ? ' (a filter with no `name` is keyed by its `field`)' : '')
149+
+ `, the reserved name of the built-in date range this dashboard declares in \`dateRange\`. `
150+
+ 'The filter bar draws one control under that name and widgets bind it as `filterBindings.dateRange`, so '
151+
+ 'this entry would replace the date range: the board would never draw the declared date range, and every '
152+
+ 'widget binding `dateRange` would be filtered by this entry instead. '
153+
+ 'Give this filter another `name` (and rename every widget `filterBindings.dateRange` key that means it), '
154+
+ 'or delete it and keep the built-in `dateRange`.',
155+
});
156+
});
157+
return out;
158+
}

0 commit comments

Comments
 (0)