grafana/public/app/features/manage-dashboards/utils/validation.ts
Peter Holmberg ec743cf9a7
Migration: Migrate Dashboard Import to React (#22338)
* first things

* introduce headers and moving buttons

* adding reducer and action for gcom dashboard

* action working

* continue building on import form

* change dashboard title

* add prop to not render a label

* first things

* introduce headers and moving buttons

* adding reducer and action for gcom dashboard

* action working

* continue building on import form

* change dashboard title

* add prop to not render a label

* import form layout

* break out form to component

* add actions and reader for file upload

* fix upload issue

* modified data types to handle both gcom and file upload

* import dashboard json

* save dashboard

* start change uid

* change dashboard uid

* fix spacing and date format

* fix import from json

* handle uid and title change

* revert change in panelinspect

* redo fileupload component

* after review

* redo forms to use Forms functionality

* first attempt on async validation

* use ternary on uid input

* removed unused actions, fixed async validation on form

* post form if invalid, break out form to component

* sync file with master

* fix after merge

* nits

* export formapi type

* redo page to use forms validation

* fix inputs and validation

* readd post

* add guards on data source and constants

* type checks and strict nulls

* strict nulls

* validate onchange and fix import button when valid

* shorten validate call

* reexport OnSubmit type

* add comment for overwrite useEffect

* move validation functions to util

* fix button imports

* remove angular import

* move title and uid validation
2020-03-31 16:29:44 +02: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;
});
};