Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Move errors and validate index pattern ⇒ NP (#51805) #51831

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,4 @@ export { SearchBar, SearchBarProps, SavedQueryAttributes, SavedQuery } from './s
/** @public static code */
export * from '../common';
export { FilterStateManager } from './filter/filter_manager';
export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
IndexPatternAlreadyExists,
IndexPatternMissingIndices,
NoDefaultIndexPattern,
NoDefinedIndexPatterns,
} from './index_patterns';
export { getFromSavedObject, getRoutes } from './index_patterns';
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import {
ES_FIELD_TYPES,
KBN_FIELD_TYPES,
IIndexPattern,
indexPatterns,
} from '../../../../../../plugins/data/public';

import { findIndexPatternByTitle, getRoutes } from '../utils';
import { IndexPatternMissingIndices } from '../errors';
import { Field, FieldList, FieldListInterface, FieldType } from '../fields';
import { createFieldsFetcher } from './_fields_fetcher';
import { formatHitProvider } from './format_hit';
Expand Down Expand Up @@ -584,7 +584,7 @@ export class IndexPattern implements IIndexPattern {
// so do not rethrow the error here
const { toasts } = getNotifications();

if (err instanceof IndexPatternMissingIndices) {
if (err instanceof indexPatterns.IndexPatternMissingIndices) {
toasts.addDanger((err as any).message);

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import {
HttpServiceBase,
} from 'kibana/public';

jest.mock('../errors', () => ({
IndexPatternMissingIndices: jest.fn(),
}));

jest.mock('./index_pattern', () => {
class IndexPattern {
init = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { HttpServiceBase } from 'src/core/public';
import { IndexPatternMissingIndices } from '../errors';
import { indexPatterns } from '../../../../../../plugins/data/public';

const API_BASE_URL: string = `/api/index_patterns/`;

Expand Down Expand Up @@ -46,7 +46,7 @@ export class IndexPatternsApiClient {
})
.catch((resp: any) => {
if (resp.body.statusCode === 404 && resp.body.statuscode === 'no_matching_indices') {
throw new IndexPatternMissingIndices(resp.body.message);
throw new indexPatterns.IndexPatternMissingIndices(resp.body.message);
}

throw new Error(resp.body.message || resp.body.error || `${resp.body.statusCode} Response`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,7 @@ export class IndexPatternsService {
// static code

/** @public */
export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
validateIndexPattern,
} from './utils';

/** @public */
export {
IndexPatternAlreadyExists,
IndexPatternMissingIndices,
NoDefaultIndexPattern,
NoDefinedIndexPatterns,
} from './errors';
export { getFromSavedObject, getRoutes } from './utils';

// types

Expand Down
41 changes: 0 additions & 41 deletions src/legacy/core_plugins/data/public/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,6 @@ import { find, get } from 'lodash';

import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';

export const ILLEGAL_CHARACTERS = 'ILLEGAL_CHARACTERS';
export const CONTAINS_SPACES = 'CONTAINS_SPACES';
export const INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE = ['\\', '/', '?', '"', '<', '>', '|'];
export const INDEX_PATTERN_ILLEGAL_CHARACTERS = INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE.concat(
' '
);

function findIllegalCharacters(indexPattern: string): string[] {
const illegalCharacters = INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE.reduce(
(chars: string[], char: string) => {
if (indexPattern.includes(char)) {
chars.push(char);
}
return chars;
},
[]
);

return illegalCharacters;
}

/**
* Returns an object matching a given title
*
Expand Down Expand Up @@ -71,26 +50,6 @@ export async function findIndexPatternByTitle(
);
}

function indexPatternContainsSpaces(indexPattern: string): boolean {
return indexPattern.includes(' ');
}

export function validateIndexPattern(indexPattern: string) {
const errors: Record<string, any> = {};

const illegalCharacters = findIllegalCharacters(indexPattern);

if (illegalCharacters.length) {
errors[ILLEGAL_CHARACTERS] = illegalCharacters;
}

if (indexPatternContainsSpaces(indexPattern)) {
errors[CONTAINS_SPACES] = true;
}

return errors;
}

export function getFromSavedObject(savedObject: any) {
if (get(savedObject, 'attributes.fields') === undefined) {
return;
Expand Down
14 changes: 1 addition & 13 deletions src/legacy/ui/public/index_patterns/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,4 @@ export const {
} = dataSetup.indexPatterns!;

// static code
export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
IndexPatternAlreadyExists,
IndexPatternMissingIndices,
NoDefaultIndexPattern,
NoDefinedIndexPatterns,
} from '../../../../core_plugins/data/public';
export { getFromSavedObject, getRoutes } from '../../../../core_plugins/data/public';
22 changes: 9 additions & 13 deletions src/legacy/ui/public/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ export const {
formatHitProvider,
} = data.indexPatterns;

import { indexPatterns } from '../../../../plugins/data/public';

// static code
export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
IndexPatternAlreadyExists,
IndexPatternMissingIndices,
NoDefaultIndexPattern,
NoDefinedIndexPatterns,
} from '../../../core_plugins/data/public';
export { getFromSavedObject, getRoutes } from '../../../core_plugins/data/public';

export const INDEX_PATTERN_ILLEGAL_CHARACTERS = indexPatterns.ILLEGAL_CHARACTERS;
export const INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE = indexPatterns.ILLEGAL_CHARACTERS_VISIBLE;
export const ILLEGAL_CHARACTERS = indexPatterns.ILLEGAL_CHARACTERS_KEY;
export const CONTAINS_SPACES = indexPatterns.CONTAINS_SPACES_KEY;
export const validateIndexPattern = indexPatterns.validate;

// types
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

jest.mock('ui/new_platform');
jest.mock('ui/index_patterns');

import { INDEX_ILLEGAL_CHARACTERS_VISIBLE } from '../constants';

Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from '../common';

export * from './autocomplete_provider';
export * from './field_formats_provider';
export * from './index_patterns';

export * from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@

/* eslint-disable */

import { KbnError } from '../../../../../plugins/kibana_utils/public';

/**
* when a mapping already exists for a field the user is attempting to add
* @param {String} name - the field name
*/
export class IndexPatternAlreadyExists extends KbnError {
constructor(name: string) {
super(`An index pattern of "${name}" already exists`);
}
}
import { KbnError } from '../../../kibana_utils/public';

/**
* Tried to call a method that relies on SearchSource having an indexPattern assigned
Expand All @@ -43,21 +33,3 @@ export class IndexPatternMissingIndices extends KbnError {
);
}
}

/**
* Tried to call a method that relies on SearchSource having an indexPattern assigned
*/
export class NoDefinedIndexPatterns extends KbnError {
constructor() {
super('Define at least one index pattern to continue');
}
}

/**
* Tried to load a route besides management/kibana/index but you don't have a default index pattern!
*/
export class NoDefaultIndexPattern extends KbnError {
constructor() {
super('Please specify a default index pattern');
}
}
36 changes: 36 additions & 0 deletions src/plugins/data/public/index_patterns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { IndexPatternMissingIndices } from './errors';
import {
ILLEGAL_CHARACTERS_KEY,
CONTAINS_SPACES_KEY,
ILLEGAL_CHARACTERS_VISIBLE,
ILLEGAL_CHARACTERS,
validateIndexPattern,
} from './lib';

export const indexPatterns = {
ILLEGAL_CHARACTERS_KEY,
CONTAINS_SPACES_KEY,
ILLEGAL_CHARACTERS_VISIBLE,
ILLEGAL_CHARACTERS,
IndexPatternMissingIndices,
validate: validateIndexPattern,
};
2 changes: 2 additions & 0 deletions src/plugins/data/public/index_patterns/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
*/

export { getIndexPatternTitle } from './get_index_pattern_title';
export * from './types';
export { validateIndexPattern } from './validate_index_pattern';
23 changes: 23 additions & 0 deletions src/plugins/data/public/index_patterns/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

export const ILLEGAL_CHARACTERS_KEY = 'ILLEGAL_CHARACTERS';
export const CONTAINS_SPACES_KEY = 'CONTAINS_SPACES';
export const ILLEGAL_CHARACTERS_VISIBLE = ['\\', '/', '?', '"', '<', '>', '|'];
export const ILLEGAL_CHARACTERS = ILLEGAL_CHARACTERS_VISIBLE.concat(' ');
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@
* under the License.
*/

import {
CONTAINS_SPACES,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
validateIndexPattern,
} from './utils';
import { CONTAINS_SPACES_KEY, ILLEGAL_CHARACTERS_KEY, ILLEGAL_CHARACTERS_VISIBLE } from './types';

import { validateIndexPattern } from './validate_index_pattern';

describe('Index Pattern Utils', () => {
describe('Validation', () => {
it('should not allow space in the pattern', () => {
const errors = validateIndexPattern('my pattern');
expect(errors[CONTAINS_SPACES]).toBe(true);
expect(errors[CONTAINS_SPACES_KEY]).toBe(true);
});

it('should not allow illegal characters', () => {
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE.forEach(char => {
ILLEGAL_CHARACTERS_VISIBLE.forEach(char => {
const errors = validateIndexPattern(`pattern${char}`);
expect(errors[ILLEGAL_CHARACTERS]).toEqual([char]);
expect(errors[ILLEGAL_CHARACTERS_KEY]).toEqual([char]);
});
});

Expand Down
Loading