[8.9] [data view editor] Fix data view timestamp validation (#150398) (#160990)

# Backport

This will backport the following commits from `main` to `8.9`:
- [[data view editor] Fix data view timestamp validation
(#150398)](https://github.com/elastic/kibana/pull/150398)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthew
Kime","email":"matt@mattki.me"},"sourceCommit":{"committedDate":"2023-06-30T12:29:44Z","message":"[data
view editor] Fix data view timestamp validation (#150398)\n\n##
Summary\r\n\r\nPreviously - If you changed a data view's index pattern
AND the new\r\npattern didn't contain the timestamp field, you'd see a
blank timestamp\r\nfield and it would let you save. The data view would
have been saved\r\nwith the previous timestamp field which doesn't
exist.\r\n\r\nNow - The timestamp validator checks to make sure the
selected timestamp\r\nfield is in the list of available options. This is
helpful because it\r\nkeeps the previous timestamp value in case you do
select an index\r\npattern that contains it.\r\n\r\nCloses:
https://github.com/elastic/kibana/issues/150219","sha":"646539c45b454b48c9fa985b7ac5cb69a63c70f8","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Feature:Data
Views","Feature:Kibana
Management","Team:DataDiscovery","backport:prev-minor","v8.10.0"],"number":150398,"url":"https://github.com/elastic/kibana/pull/150398","mergeCommit":{"message":"[data
view editor] Fix data view timestamp validation (#150398)\n\n##
Summary\r\n\r\nPreviously - If you changed a data view's index pattern
AND the new\r\npattern didn't contain the timestamp field, you'd see a
blank timestamp\r\nfield and it would let you save. The data view would
have been saved\r\nwith the previous timestamp field which doesn't
exist.\r\n\r\nNow - The timestamp validator checks to make sure the
selected timestamp\r\nfield is in the list of available options. This is
helpful because it\r\nkeeps the previous timestamp value in case you do
select an index\r\npattern that contains it.\r\n\r\nCloses:
https://github.com/elastic/kibana/issues/150219","sha":"646539c45b454b48c9fa985b7ac5cb69a63c70f8"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/150398","number":150398,"mergeCommit":{"message":"[data
view editor] Fix data view timestamp validation (#150398)\n\n##
Summary\r\n\r\nPreviously - If you changed a data view's index pattern
AND the new\r\npattern didn't contain the timestamp field, you'd see a
blank timestamp\r\nfield and it would let you save. The data view would
have been saved\r\nwith the previous timestamp field which doesn't
exist.\r\n\r\nNow - The timestamp validator checks to make sure the
selected timestamp\r\nfield is in the list of available options. This is
helpful because it\r\nkeeps the previous timestamp value in case you do
select an index\r\npattern that contains it.\r\n\r\nCloses:
https://github.com/elastic/kibana/issues/150219","sha":"646539c45b454b48c9fa985b7ac5cb69a63c70f8"}}]}]
BACKPORT-->

Co-authored-by: Matthew Kime <matt@mattki.me>
This commit is contained in:
Kibana Machine 2023-06-30 09:36:29 -04:00 committed by GitHub
parent 3ff0172146
commit 9a3dd16bd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 5 deletions

View file

@ -299,6 +299,8 @@ const IndexPatternEditorFlyoutContentComponent = ({
form.updateFieldValues({ name: formData.title });
await form.getFields().name.validate();
}
// Ensures timestamp field is validated against current set of options
form.validateFields(['timestampField']);
form.setFieldValue('isAdHoc', adhoc || false);
form.submit();
}}

View file

@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { requireTimestampOptionValidator } from './timestamp_field';
import { TimestampOption } from '../../types';
const noOptions: TimestampOption[] = [];
const options: TimestampOption[] = [
{ display: 'a', fieldName: 'a' },
{ display: 'a', fieldName: 'b' },
];
describe('timestamp field validator', () => {
test('no timestamp options - pass without value', async () => {
const result = await requireTimestampOptionValidator(noOptions).validator({
value: undefined,
} as any);
// no error
expect(result).toBeUndefined();
});
test('timestamp options - fail without value', async () => {
const result = await requireTimestampOptionValidator(options).validator({
value: undefined,
} as any);
// returns error
expect(result).toBeDefined();
});
test('timestamp options - pass with value', async () => {
const result = await requireTimestampOptionValidator(options).validator({
value: { label: 'a', value: 'a' },
} as any);
// no error
expect(result).toBeUndefined();
});
test('timestamp options - fail, value not in list', async () => {
const result = await requireTimestampOptionValidator(options).validator({
value: { label: 'c', value: 'c' },
} as any);
// returns error
expect(result).toBeDefined();
});
});

View file

@ -29,10 +29,13 @@ interface Props {
matchedIndices$: Observable<MatchedIndicesSet>;
}
const requireTimestampOptionValidator = (options: TimestampOption[]): ValidationConfig => ({
validator: async ({ value }) => {
export const requireTimestampOptionValidator = (
options: TimestampOption[]
): ValidationConfig<any, string, { value?: any }> => ({
validator: async ({ value: selectedOption }) => {
const isValueRequired = !!options.length;
if (isValueRequired && !value) {
const valueSelected = options.find((item) => item.fieldName === selectedOption?.value);
if (isValueRequired && (!selectedOption || !valueSelected)) {
return {
message: i18n.translate(
'indexPatternEditor.requireTimestampOption.ValidationErrorMessage',

View file

@ -19,12 +19,13 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const find = getService('find');
const PageObjects = getPageObjects(['settings', 'common', 'header']);
describe('creating and deleting default index', function describeIndexTests() {
describe('creating and deleting default data view', function describeIndexTests() {
before(async function () {
await esArchiver.emptyKibanaIndex();
await esArchiver.loadIfNeeded(
'test/functional/fixtures/es_archiver/kibana_sample_data_flights_index_pattern'
);
await esArchiver.loadIfNeeded('test/functional/fixtures/es_archiver/logstash_functional');
await kibanaServer.uiSettings.replace({});
await PageObjects.settings.navigateTo();
await PageObjects.settings.clickKibanaIndexPatterns();
@ -34,6 +35,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await esArchiver.unload(
'test/functional/fixtures/es_archiver/kibana_sample_data_flights_index_pattern'
);
await esArchiver.unload('test/functional/fixtures/es_archiver/logstash_functional');
});
describe('can open and close editor', function () {
@ -59,6 +62,23 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await (await PageObjects.settings.getSaveDataViewButtonActive()).click();
await PageObjects.settings.removeIndexPattern();
});
it('correctly validates timestamp after index pattern changes', async function () {
await PageObjects.settings.clickKibanaIndexPatterns();
await PageObjects.settings.clickAddNewIndexPatternButton();
// setting the index pattern also sets the time field
await PageObjects.settings.setIndexPatternField('log*');
// wait for timestamp fields to load
await new Promise((e) => setTimeout(e, 1000));
// this won't have 'timestamp' field
await PageObjects.settings.setIndexPatternField('kibana*');
// wait for timestamp fields to load
await new Promise((e) => setTimeout(e, 1000));
await (await PageObjects.settings.getSaveIndexPatternButton()).click();
// verify an error is displayed
await find.byClassName('euiFormErrorText');
await testSubjects.click('closeFlyoutButton');
});
});
describe('special character handling', () => {

View file

@ -22,7 +22,7 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) {
});
loadTestFile(require.resolve('./_create_index_pattern_wizard'));
loadTestFile(require.resolve('./_index_pattern_create_delete'));
loadTestFile(require.resolve('./_data_view_create_delete'));
loadTestFile(require.resolve('./_index_pattern_results_sort'));
loadTestFile(require.resolve('./_index_pattern_popularity'));
loadTestFile(require.resolve('./_kibana_settings'));