validation.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { validationSrv } from '../services/ValidationSrv';
  3. export const validateDashboardJson = (json: string) => {
  4. try {
  5. JSON.parse(json);
  6. return true;
  7. } catch (error) {
  8. return 'Not valid JSON';
  9. }
  10. };
  11. export const validateGcomDashboard = (gcomDashboard: string) => {
  12. // From DashboardImportCtrl
  13. const match = /(^\d+$)|dashboards\/(\d+)/.exec(gcomDashboard);
  14. return match && (match[1] || match[2]) ? true : 'Could not find a valid Grafana.com ID';
  15. };
  16. export const validateTitle = (newTitle: string, folderId: number) => {
  17. return validationSrv
  18. .validateNewDashboardName(folderId, newTitle)
  19. .then(() => {
  20. return true;
  21. })
  22. .catch((error) => {
  23. if (error.type === 'EXISTING') {
  24. return error.message;
  25. }
  26. });
  27. };
  28. export const validateUid = (value: string) => {
  29. return getBackendSrv()
  30. .get(`/api/dashboards/uid/${value}`)
  31. .then((existingDashboard) => {
  32. return `Dashboard named '${existingDashboard?.dashboard.title}' in folder '${existingDashboard?.meta.folderTitle}' has the same UID`;
  33. })
  34. .catch((error) => {
  35. error.isHandled = true;
  36. return true;
  37. });
  38. };