fix frontend validation for creating new folder and import dashboard (#10737)

Fixes #10731
This commit is contained in:
Marcus Efraimsson 2018-02-03 10:03:01 +01:00 committed by Torkel Ödegaard
parent a0d8e96fd1
commit 4b64c1d0d4
5 changed files with 38 additions and 10 deletions

View File

@ -27,7 +27,7 @@ export class CreateFolderCtrl {
this.titleTouched = true; this.titleTouched = true;
this.validationSrv this.validationSrv
.validateNewDashboardOrFolderName(this.title) .validateNewFolderName(this.title)
.then(() => { .then(() => {
this.hasValidationError = false; this.hasValidationError = false;
}) })

View File

@ -93,7 +93,7 @@ export class DashboardImportCtrl {
this.nameExists = false; this.nameExists = false;
this.validationSrv this.validationSrv
.validateNewDashboardOrFolderName(this.dash.title) .validateNewDashboardName(0, this.dash.title)
.then(() => { .then(() => {
this.hasNameValidationError = false; this.hasNameValidationError = false;
}) })

View File

@ -67,7 +67,7 @@ export class FolderPickerCtrl {
this.newFolderNameTouched = true; this.newFolderNameTouched = true;
this.validationSrv this.validationSrv
.validateNewDashboardOrFolderName(this.newFolderName) .validateNewFolderName(this.newFolderName)
.then(() => { .then(() => {
this.hasValidationError = false; this.hasValidationError = false;
}) })

View File

@ -19,7 +19,7 @@ describe('DashboardImportCtrl', function() {
}; };
validationSrv = { validationSrv = {
validateNewDashboardOrFolderName: jest.fn().mockReturnValue(Promise.resolve()), validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
}; };
ctx.ctrl = new DashboardImportCtrl(backendSrv, validationSrv, navModelSrv, {}, {}, {}); ctx.ctrl = new DashboardImportCtrl(backendSrv, validationSrv, navModelSrv, {}, {}, {});

View File

@ -1,13 +1,27 @@
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
const hitTypes = {
FOLDER: 'dash-folder',
DASHBOARD: 'dash-db',
};
export class ValidationSrv { export class ValidationSrv {
rootName = 'general'; rootName = 'general';
/** @ngInject */ /** @ngInject */
constructor(private $q, private backendSrv) {} constructor(private $q, private backendSrv) {}
validateNewDashboardOrFolderName(name) { validateNewDashboardName(folderId, name) {
return this.validate(folderId, name, 'A dashboard in this folder with the same name already exists');
}
validateNewFolderName(name) {
return this.validate(0, name, 'A folder or dashboard in the general folder with the same name already exists');
}
private validate(folderId, name, existingErrorMessage) {
name = (name || '').trim(); name = (name || '').trim();
const nameLowerCased = name.toLowerCase();
if (name.length === 0) { if (name.length === 0) {
return this.$q.reject({ return this.$q.reject({
@ -16,7 +30,7 @@ export class ValidationSrv {
}); });
} }
if (name.toLowerCase() === this.rootName) { if (folderId === 0 && nameLowerCased === this.rootName) {
return this.$q.reject({ return this.$q.reject({
type: 'EXISTING', type: 'EXISTING',
message: 'This is a reserved name and cannot be used for a folder.', message: 'This is a reserved name and cannot be used for a folder.',
@ -25,12 +39,26 @@ export class ValidationSrv {
let deferred = this.$q.defer(); let deferred = this.$q.defer();
this.backendSrv.search({ query: name }).then(res => { const promises = [];
for (let hit of res) { promises.push(this.backendSrv.search({ type: hitTypes.FOLDER, folderIds: [folderId], query: name }));
if (name.toLowerCase() === hit.title.toLowerCase()) { promises.push(this.backendSrv.search({ type: hitTypes.DASHBOARD, folderIds: [folderId], query: name }));
this.$q.all(promises).then(res => {
let hits = [];
if (res.length > 0 && res[0].length > 0) {
hits = res[0];
}
if (res.length > 1 && res[1].length > 0) {
hits = hits.concat(res[1]);
}
for (let hit of hits) {
if (nameLowerCased === hit.title.toLowerCase()) {
deferred.reject({ deferred.reject({
type: 'EXISTING', type: 'EXISTING',
message: 'A folder or dashboard with the same name already exists', message: existingErrorMessage,
}); });
break; break;
} }