Public source validation / validate (push) Failing after 3m8s
25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
type DashboardVariable = { name?: unknown; default?: unknown };
|
|
|
|
// Resolve only exact `$name` references declared by the dashboard. The query
|
|
// planner remains the authority for accepted scope keys and values; unresolved
|
|
// references are preserved so they fail visibly instead of widening a query.
|
|
export function resolveDashboardScope(
|
|
rawScope: Record<string, unknown>,
|
|
variables: unknown[],
|
|
): Record<string, string> {
|
|
const defaults = new Map<string, string>();
|
|
for (const candidate of variables) {
|
|
if (!candidate || typeof candidate !== 'object') continue;
|
|
const variable = candidate as DashboardVariable;
|
|
if (typeof variable.name === 'string' && variable.name !== '' && typeof variable.default === 'string') {
|
|
defaults.set(variable.name, variable.default);
|
|
}
|
|
}
|
|
return Object.fromEntries(Object.entries(rawScope).flatMap(([key, value]) => {
|
|
if (typeof value !== 'string') return [];
|
|
const reference = /^\$([A-Za-z][A-Za-z0-9_-]{0,63})$/.exec(value);
|
|
if (!reference) return [[key, value]];
|
|
return [[key, defaults.get(reference[1]) ?? value]];
|
|
}));
|
|
}
|