2020-01-21 03:08:07 -06:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2017-12-19 06:33:34 -06:00
|
|
|
|
2018-02-03 03:03:01 -06:00
|
|
|
const hitTypes = {
|
|
|
|
FOLDER: 'dash-folder',
|
|
|
|
DASHBOARD: 'dash-db',
|
|
|
|
};
|
|
|
|
|
2022-11-08 08:11:02 -06:00
|
|
|
class ValidationError extends Error {
|
|
|
|
type: string;
|
|
|
|
|
|
|
|
constructor(type: string, message: string) {
|
|
|
|
super(message);
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 06:33:34 -06:00
|
|
|
export class ValidationSrv {
|
2018-02-02 03:33:31 -06:00
|
|
|
rootName = 'general';
|
2017-12-19 06:33:34 -06:00
|
|
|
|
2022-11-17 02:22:57 -06:00
|
|
|
validateNewDashboardName(folderUid: any, name: string) {
|
|
|
|
return this.validate(folderUid, name, 'A dashboard or a folder with the same name already exists');
|
2018-02-03 03:03:01 -06:00
|
|
|
}
|
|
|
|
|
2021-04-15 07:21:06 -05:00
|
|
|
validateNewFolderName(name?: string) {
|
2018-02-03 03:03:01 -06:00
|
|
|
return this.validate(0, name, 'A folder or dashboard in the general folder with the same name already exists');
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:21:06 -05:00
|
|
|
private async validate(folderId: any, name: string | undefined, existingErrorMessage: string) {
|
2017-12-20 05:33:33 -06:00
|
|
|
name = (name || '').trim();
|
2018-02-03 03:03:01 -06:00
|
|
|
const nameLowerCased = name.toLowerCase();
|
2017-12-19 06:33:34 -06:00
|
|
|
|
|
|
|
if (name.length === 0) {
|
2022-11-08 08:11:02 -06:00
|
|
|
throw new ValidationError('REQUIRED', 'Name is required');
|
2017-12-19 06:33:34 -06:00
|
|
|
}
|
|
|
|
|
2018-02-03 03:03:01 -06:00
|
|
|
if (folderId === 0 && nameLowerCased === this.rootName) {
|
2022-11-08 08:11:02 -06:00
|
|
|
throw new ValidationError('EXISTING', 'This is a reserved name and cannot be used for a folder.');
|
2017-12-19 06:33:34 -06:00
|
|
|
}
|
|
|
|
|
2018-02-03 03:03:01 -06:00
|
|
|
const promises = [];
|
2020-01-21 03:08:07 -06:00
|
|
|
promises.push(backendSrv.search({ type: hitTypes.FOLDER, folderIds: [folderId], query: name }));
|
|
|
|
promises.push(backendSrv.search({ type: hitTypes.DASHBOARD, folderIds: [folderId], query: name }));
|
2018-02-03 03:03:01 -06:00
|
|
|
|
2020-02-13 04:13:03 -06:00
|
|
|
const res = await Promise.all(promises);
|
|
|
|
let hits: any[] = [];
|
2018-02-03 03:03:01 -06:00
|
|
|
|
2020-02-13 04:13:03 -06:00
|
|
|
if (res.length > 0 && res[0].length > 0) {
|
|
|
|
hits = res[0];
|
|
|
|
}
|
2018-02-03 03:03:01 -06:00
|
|
|
|
2020-02-13 04:13:03 -06:00
|
|
|
if (res.length > 1 && res[1].length > 0) {
|
|
|
|
hits = hits.concat(res[1]);
|
|
|
|
}
|
2018-02-03 03:03:01 -06:00
|
|
|
|
2020-02-13 04:13:03 -06:00
|
|
|
for (const hit of hits) {
|
|
|
|
if (nameLowerCased === hit.title.toLowerCase()) {
|
2022-11-08 08:11:02 -06:00
|
|
|
throw new ValidationError('EXISTING', existingErrorMessage);
|
2017-12-19 06:33:34 -06:00
|
|
|
}
|
2020-02-13 04:13:03 -06:00
|
|
|
}
|
2017-12-19 06:33:34 -06:00
|
|
|
|
2020-02-13 04:13:03 -06:00
|
|
|
return;
|
2017-12-19 06:33:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 08:12:45 -06:00
|
|
|
export const validationSrv = new ValidationSrv();
|