grafana/public/app/features/manage-dashboards/utils/validation.ts
Torkel Ödegaard 1d689888b0
Prettier: Upgrade to 2 (#30387)
* Updated package json but not updated source files

* Update eslint plugin

* updated files
2021-01-20 07:59:48 +01:00

44 lines
1.2 KiB
TypeScript

import validationSrv from '../services/ValidationSrv';
import { getBackendSrv } from '@grafana/runtime';
export const validateDashboardJson = (json: string) => {
try {
JSON.parse(json);
return true;
} catch (error) {
return 'Not valid JSON';
}
};
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, folderId: number) => {
return validationSrv
.validateNewDashboardName(folderId, 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;
});
};