forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Vis: Default editor] EUIficate raw json control (elastic#32888)
* EUIficate raw json control * Remove unused validate-json directive * Update tests * Update styles * Move validation logic down to control * Fix type
- Loading branch information
1 parent
519840a
commit 41f032e
Showing
13 changed files
with
203 additions
and
204 deletions.
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
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,49 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { isValidJson } from '../utils'; | ||
|
||
const input = { | ||
valid: '{ "test": "json input" }', | ||
invalid: 'strings are not json', | ||
}; | ||
|
||
describe('AggType utils', () => { | ||
describe('isValidJson', () => { | ||
it('should return true when empty string', () => { | ||
expect(isValidJson('')).toBe(true); | ||
}); | ||
|
||
it('should return true when undefine', () => { | ||
expect(isValidJson(undefined as any)).toBe(true); | ||
}); | ||
|
||
it('should return false when invalid string', () => { | ||
expect(isValidJson(input.invalid)).toBe(false); | ||
}); | ||
|
||
it('should return true when valid string', () => { | ||
expect(isValidJson(input.valid)).toBe(true); | ||
}); | ||
|
||
it('should return false if a number', () => { | ||
expect(isValidJson('0')).toBe(false); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { EuiFormRow, EuiIconTip, EuiTextArea } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { AggParamEditorProps } from '../../vis/editors/default'; | ||
import { isValidJson } from '../utils'; | ||
|
||
function RawJsonParamEditor({ | ||
agg, | ||
value, | ||
setValue, | ||
isInvalid, | ||
setValidity, | ||
}: AggParamEditorProps<string>) { | ||
const label = ( | ||
<> | ||
<FormattedMessage id="common.ui.aggTypes.jsonInputLabel" defaultMessage="JSON input" />{' '} | ||
<EuiIconTip | ||
position="right" | ||
content={i18n.translate('common.ui.aggTypes.jsonInputTooltip', { | ||
defaultMessage: | ||
"Any JSON formatted properties you add here will be merged with the elasticsearch aggregation definition for this section. For example 'shard_size' on a terms aggregation.", | ||
})} | ||
type="questionInCircle" | ||
/> | ||
</> | ||
); | ||
|
||
const onChange = (ev: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
const textValue = ev.target.value; | ||
setValue(textValue); | ||
setValidity(isValidJson(textValue)); | ||
}; | ||
|
||
setValidity(isValidJson(value)); | ||
|
||
return ( | ||
<EuiFormRow | ||
label={label} | ||
isInvalid={isInvalid} | ||
fullWidth={true} | ||
className="visEditorSidebar__aggParamFormRow" | ||
> | ||
<EuiTextArea | ||
id={`visEditorRawJson${agg.id}`} | ||
isInvalid={isInvalid} | ||
value={value || ''} | ||
onChange={onChange} | ||
rows={2} | ||
fullWidth={true} | ||
/> | ||
</EuiFormRow> | ||
); | ||
} | ||
|
||
export { RawJsonParamEditor }; |
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,39 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
function isValidJson(value: string): boolean { | ||
if (!value || value.length === 0) { | ||
return true; | ||
} | ||
|
||
const trimmedValue = value.trim(); | ||
|
||
if (trimmedValue[0] === '{' || trimmedValue[0] === '[') { | ||
try { | ||
JSON.parse(trimmedValue); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export { isValidJson }; |
111 changes: 0 additions & 111 deletions
111
src/legacy/ui/public/directives/__tests__/validate_json.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.