[data views] don't allow single * index pattern (#124906) (#125022)

* don't allow single astrisk

* Update form_schema.ts

* add tests

(cherry picked from commit 330c3e21ac)

Co-authored-by: Matthew Kime <matt@mattki.me>
This commit is contained in:
Kibana Machine 2022-02-08 20:17:59 -05:00 committed by GitHub
parent 1d9e036c0b
commit df3eb51f7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View file

@ -0,0 +1,22 @@
/*
* 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 { singleAstriskValidator } from './form_schema';
import { ValidationFuncArg } from '../shared_imports';
describe('validators', () => {
test('singleAstriskValidator should pass', async () => {
const result = singleAstriskValidator({ value: 'kibana*' } as ValidationFuncArg<any, any>);
expect(result).toBeUndefined();
});
test('singleAstriskValidator should fail', async () => {
const result = singleAstriskValidator({ value: '*' } as ValidationFuncArg<any, any>);
// returns error
expect(result).toBeDefined();
});
});

View file

@ -7,9 +7,21 @@
*/
import { i18n } from '@kbn/i18n';
import { fieldValidators } from '../shared_imports';
import { fieldValidators, ValidationFunc } from '../shared_imports';
import { INDEX_PATTERN_TYPE } from '../types';
export const singleAstriskValidator = (
...args: Parameters<ValidationFunc>
): ReturnType<ValidationFunc> => {
const [{ value, path }] = args;
const message = i18n.translate('indexPatternEditor.validations.noSingleAstriskPattern', {
defaultMessage: "A single '*' is not an allowed index pattern",
});
return value === '*' ? { code: 'ERR_FIELD_MISSING', path, message } : undefined;
};
export const schema = {
title: {
label: i18n.translate('indexPatternEditor.editor.form.titleLabel', {
@ -28,6 +40,9 @@ export const schema = {
})
),
},
{
validator: singleAstriskValidator,
},
],
},
timestampField: {

View file

@ -28,6 +28,7 @@ export type {
ValidationFunc,
FieldConfig,
ValidationConfig,
ValidationFuncArg,
} from '../../es_ui_shared/static/forms/hook_form_lib';
export {
useForm,