grafana/public/app/features/manage-dashboards/utils/validation.ts
Diana Payton 1399b49c16
UI text edits (#32524)
* Update TeamSettings.tsx

* Update navModel.ts

* Update ChangePasswordForm.tsx

* Update UserProfileEditForm.tsx

* Update DashboardImportPage.tsx

* Update uploadDashboardDirective.ts

* Update ImportDashboardForm.tsx

* Update ImportDashboardOverview.tsx

* Update validation.ts

* Update PlaylistEditPage.tsx

* ui text edits

* text edits

* Update buildCategories.ts

* text edits

* text edits

* Fix formatting

* Update test snapshots

Co-authored-by: dsotirakis <sotirakis.dim@gmail.com>
2021-03-31 16:07:37 -07: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;
});
};