mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* support folderuid in FolderPicker * support folderuid in unified alerting * support folderuid when returning to view mode after editing a panel * support folderuid when preselecting the folderpicker in dashboard general settings * support folderuid when saving dashboard * support folderuid when pre-selecting folderpicker in dashboard form * support folderuid in routes when loading a dashboard * support folderuid when saving dashboard json * support folderuid when validating new dashboard name * support folderuid when moving dashboard to another folder * support folderuid on dashboard action buttons * support folderuid when creating a new dashboard on an empty folder * support folderuid when showing library panel modal * support folderuid when saving library panel * support folderuid when importing dashboard * fixed broken tests * use folderuid when importing dashboards * remove commented line * fix typo when comparing uid values
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
import { validationSrv } from '../services/ValidationSrv';
|
|
|
|
export const validateDashboardJson = (json: string) => {
|
|
let dashboard;
|
|
try {
|
|
dashboard = JSON.parse(json);
|
|
} catch (error) {
|
|
return 'Not valid JSON';
|
|
}
|
|
if (dashboard && dashboard.hasOwnProperty('tags')) {
|
|
if (Array.isArray(dashboard.tags)) {
|
|
const hasInvalidTag = dashboard.tags.some((tag: string) => typeof tag !== 'string');
|
|
if (hasInvalidTag) {
|
|
return 'tags expected array of strings';
|
|
}
|
|
} else {
|
|
return 'tags expected array';
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
export const validateGcomDashboard = (gcomDashboard: string) => {
|
|
// From DashboardImportCtrl
|
|
const match = /(^\d+$)|dashboards\/(\d+)/.exec(gcomDashboard);
|
|
|
|
return match && (match[1] || match[2]) ? true : 'Could not find a valid Grafana.com ID';
|
|
};
|
|
|
|
export const validateTitle = (newTitle: string, folderUid: string) => {
|
|
return validationSrv
|
|
.validateNewDashboardName(folderUid, newTitle)
|
|
.then(() => {
|
|
return true;
|
|
})
|
|
.catch((error) => {
|
|
if (error.type === 'EXISTING') {
|
|
return error.message;
|
|
}
|
|
});
|
|
};
|
|
|
|
export const validateUid = (value: string) => {
|
|
return getBackendSrv()
|
|
.get(`/api/dashboards/uid/${value}`)
|
|
.then((existingDashboard) => {
|
|
return `Dashboard named '${existingDashboard?.dashboard.title}' in folder '${existingDashboard?.meta.folderTitle}' has the same UID`;
|
|
})
|
|
.catch((error) => {
|
|
error.isHandled = true;
|
|
return true;
|
|
});
|
|
};
|