kibana-123
Version:
Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic
48 lines (41 loc) • 1.08 kB
JavaScript
import _ from 'lodash';
import $ from 'jquery';
import uiModules from 'ui/modules';
let module = uiModules.get('kibana');
module.directive('validateJson', function ($compile) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
'ngModel': '=',
'queryInput': '=?',
},
link: function ($scope, $elem, attr, ngModel) {
$scope.$watch('ngModel', validator);
function validator(newValue, oldValue) {
if (!newValue || newValue.length === 0) {
setValid();
return;
}
// We actually need a proper object in all JSON inputs
newValue = (newValue || '').trim();
if (newValue[0] === '{' || '[') {
try {
JSON.parse(newValue);
setValid();
} catch (e) {
setInvalid();
}
} else {
setInvalid();
}
}
function setValid() {
ngModel.$setValidity('jsonInput', true);
}
function setInvalid() {
ngModel.$setValidity('jsonInput', false);
}
}
};
});