fix json validation and empty json handling in advanced settings (#15531) (#15644)

This commit is contained in:
Chris Roberson 2017-12-18 09:10:53 -05:00 committed by GitHub
parent df790a42c1
commit 7173053102
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 1 deletions

View file

@ -64,6 +64,7 @@
ng-keyup="maybeCancel($event, conf)"
elastic-textarea
validate-json
data-test-subj="unsavedValueJsonTextArea"
></textarea>
<p

View file

@ -44,6 +44,12 @@ uiModules.get('apps/management')
};
$scope.save = function (conf) {
// an empty JSON is valid as per the validateJson directive.
// set the value to empty JSON in this case so that its parsing upon retrieving the setting does not fail.
if (conf.type === 'json' && conf.unsavedValue === '') {
conf.unsavedValue = '{}';
}
loading(conf, function () {
if (conf.unsavedValue === conf.defVal) {
return config.remove(conf.name);

View file

@ -78,6 +78,10 @@ describe('validate-json directive', function () {
checkValid(input.invalid, 'ng-invalid');
});
it('should be invalid if a number', function () {
checkValid('0', 'ng-invalid');
});
it('should update validity on changes', function () {
checkValid(input.valid, 'ng-valid');
checkValid(input.invalid, 'ng-invalid');

View file

@ -21,7 +21,7 @@ module.directive('validateJson', function () {
// We actually need a proper object in all JSON inputs
newValue = (newValue || '').trim();
if (newValue[0] === '{' || '[') {
if (newValue[0] === '{' || newValue[0] === '[') {
try {
JSON.parse(newValue);
setValid();

View file

@ -28,6 +28,13 @@ export default function ({ getService, getPageObjects }) {
expect(advancedSetting).to.be('America/Phoenix');
});
it('should coerce an empty setting of type JSON into an empty object', async function () {
await PageObjects.settings.clickKibanaSettings();
await PageObjects.settings.setAdvancedSettingsInput('query:queryString:options', '', 'unsavedValueJsonTextArea');
const advancedSetting = await PageObjects.settings.getAdvancedSettings('query:queryString:options');
expect(advancedSetting).to.be.eql('{}');
});
describe('state:storeInSessionStorage', () => {
it ('defaults to false', async () => {
await PageObjects.settings.clickKibanaSettings();