This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Index name pattern validation directive
- Loading branch information
1 parent
69aa107
commit 6c2ac8b
Showing
4 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// See https://github.com/elasticsearch/elasticsearch/issues/6736 | ||
define(function (require) { | ||
var _ = require('lodash'); | ||
|
||
require('modules') | ||
.get('kibana') | ||
.directive('validateIndexName', function () { | ||
return { | ||
restrict: 'A', | ||
require: 'ngModel', | ||
scope: { | ||
'ngModel': '=' | ||
}, | ||
link: function ($scope, elem, attr, ngModel) { | ||
var illegalCharacters = ['\\', '/', '?', '"', '<', '>', '|', ' ', ',']; | ||
var isValid = function (input) { | ||
if (input == null || input === '' || input === '.' || input === '..') return false; | ||
|
||
var match = _.find(illegalCharacters, function (character) { | ||
return input.indexOf(character) >= 0; | ||
}); | ||
return !match; | ||
}; | ||
|
||
// From User | ||
ngModel.$parsers.unshift(function (value) { | ||
var valid = isValid(value); | ||
ngModel.$setValidity('indexNameInput', valid); | ||
return valid ? value : undefined; | ||
}); | ||
|
||
// To user | ||
ngModel.$formatters.unshift(function (value) { | ||
ngModel.$setValidity('indexNameInput', isValid(value)); | ||
return value; | ||
}); | ||
|
||
} | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
define(function (require) { | ||
var angular = require('angular'); | ||
|
||
// Load the kibana app dependencies. | ||
require('directives/validate_index_name'); | ||
|
||
describe('Validate index name directive', function () { | ||
var $compile, $rootScope; | ||
var html = '<input type="text" ng-model="indexName" validate-index-name />'; | ||
|
||
beforeEach(module('kibana')); | ||
|
||
beforeEach(inject(function (_$compile_, _$rootScope_) { | ||
$compile = _$compile_; | ||
$rootScope = _$rootScope_; | ||
})); | ||
|
||
it('should not accept null index name patterns', function () { | ||
$rootScope.indexName = null; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept undefined index name patterns', function () { | ||
$rootScope.indexName = undefined; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept empty index name patterns', function () { | ||
$rootScope.indexName = ''; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept . as an index name pattern', function () { | ||
$rootScope.indexName = '.'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept .. as an index name pattern', function () { | ||
$rootScope.indexName = '..'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept \\ in an index name pattern', function () { | ||
$rootScope.indexName = 'foo\\bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept / in an index name pattern', function () { | ||
$rootScope.indexName = 'foo/bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept ? in an index name pattern', function () { | ||
$rootScope.indexName = 'foo?bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept " in an index name pattern', function () { | ||
$rootScope.indexName = 'foo"bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept < in an index name pattern', function () { | ||
$rootScope.indexName = 'foo<bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept > in an index name pattern', function () { | ||
$rootScope.indexName = 'foo>bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept | in an index name pattern', function () { | ||
$rootScope.indexName = 'foo|bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept spaces in an index name pattern', function () { | ||
$rootScope.indexName = 'foo bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should not accept , in an index name pattern', function () { | ||
$rootScope.indexName = 'foo,bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.be.ok(); | ||
}); | ||
|
||
it('should accept a valid index name pattern', function () { | ||
$rootScope.indexName = 'foo.bar'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.not.be.ok(); | ||
}); | ||
|
||
it('should accept * in an index name pattern', function () { | ||
$rootScope.indexName = 'foo.*'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.not.be.ok(); | ||
}); | ||
|
||
it('should accept [] in an index name pattern', function () { | ||
$rootScope.indexName = '[foo]-YYYY.MM.DD'; | ||
var element = $compile(html)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.hasClass('ng-invalid')).to.not.be.ok(); | ||
}); | ||
}); | ||
}); |