Skip to content

Commit

Permalink
Extract index pattern validation rules into ui/public (elastic#22606) (
Browse files Browse the repository at this point in the history
…elastic#22680)

* Rename containsInvalidCharacters to containsIllegalCharacters and return a value which accurately reflects the name.
* Move illegal index pattern characters from Management into ui/public/index_patterns.
  • Loading branch information
cjcenizal committed Sep 5, 2018
1 parent e6d036e commit 3cf9144
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const unmountComponentAtNode = jest.fn();

jest.doMock('react-dom', () => ({ render, unmountComponentAtNode }));

// If we don't mock this, Jest fails with the error `TypeError: Cannot redefine property: prototype
// at Function.defineProperties`.
jest.mock('ui/index_patterns', () => ({
INDEX_PATTERN_ILLEGAL_CHARACTERS: ['\\', '/', '?', '"', '<', '>', '|', ' '],
}));

jest.mock('ui/chrome', () => ({
getUiSettingsClient: () => ({
get: () => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jest.mock('../../../lib/ensure_minimum_time', () => ({
ensureMinimumTime: async (promises) => Array.isArray(promises) ? await Promise.all(promises) : await promises
}));

// If we don't mock this, Jest fails with the error `TypeError: Cannot redefine property: prototype
// at Function.defineProperties`.
jest.mock('ui/index_patterns', () => ({
INDEX_PATTERN_ILLEGAL_CHARACTERS: ['\\', '/', '?', '"', '<', '>', '|', ' '],
}));

jest.mock('ui/chrome', () => ({
getUiSettingsClient: () => ({
get: () => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { ILLEGAL_CHARACTERS, MAX_SEARCH_SIZE } from '../../constants';
import { INDEX_PATTERN_ILLEGAL_CHARACTERS as ILLEGAL_CHARACTERS } from 'ui/index_patterns';
import { MAX_SEARCH_SIZE } from '../../constants';
import {
getIndices,
containsInvalidCharacters,
containsIllegalCharacters,
getMatchedIndices,
canAppendWildcard,
ensureMinimumTime
Expand Down Expand Up @@ -240,7 +241,7 @@ export class StepIndexPatternComponent extends Component {
// This is an error scenario but do not report an error
containsErrors = true;
}
else if (!containsInvalidCharacters(query, ILLEGAL_CHARACTERS)) {
else if (containsIllegalCharacters(query, ILLEGAL_CHARACTERS)) {
const errorMessage = intl.formatMessage(
{
id: 'kbn.management.createIndexPattern.step.invalidCharactersErrorMessage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ export const MAX_NUMBER_OF_MATCHING_INDICES = 100;
export const MAX_SEARCH_SIZE = MAX_NUMBER_OF_MATCHING_INDICES + ESTIMATED_NUMBER_OF_SYSTEM_INDICES;

export const PER_PAGE_INCREMENTS = [5, 10, 20, 50];
export const ILLEGAL_CHARACTERS = ['\\', '/', '?', '"', '<', '>', '|', ' '];
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
* under the License.
*/

import { containsInvalidCharacters } from '../contains_invalid_characters';
import { containsIllegalCharacters } from '../contains_illegal_characters';

describe('containsInvalidCharacters', () => {
it('should fail with illegal characters', () => {
const valid = containsInvalidCharacters('abc', ['a']);
expect(valid).toBeFalsy();
describe('containsIllegalCharacters', () => {
it('returns true with illegal characters', () => {
const isInvalid = containsIllegalCharacters('abc', ['a']);
expect(isInvalid).toBe(true);
});

it('should pass with no illegal characters', () => {
const valid = containsInvalidCharacters('abc', ['%']);
expect(valid).toBeTruthy();
it('returns false with no illegal characters', () => {
const isInvalid = containsIllegalCharacters('abc', ['%']);
expect(isInvalid).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/

export function containsInvalidCharacters(pattern, illegalCharacters) {
return !illegalCharacters.some(char => pattern.includes(char));
export function containsIllegalCharacters(pattern, illegalCharacters) {
return illegalCharacters.some(char => pattern.includes(char));
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export { getIndices } from './get_indices';

export { getMatchedIndices } from './get_matched_indices';

export { containsInvalidCharacters } from './contains_invalid_characters';
export { containsIllegalCharacters } from './contains_illegal_characters';

export { extractTimeFields } from './extract_time_fields';
21 changes: 21 additions & 0 deletions src/ui/public/index_patterns/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE = ['\\', '/', '?', '"', '<', '>', '|'];
export const INDEX_PATTERN_ILLEGAL_CHARACTERS = INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE.concat(' ');
6 changes: 6 additions & 0 deletions src/ui/public/index_patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/

export { IndexPatternsProvider } from './index_patterns';

export {
IndexPatternsApiClientProvider,
} from './index_patterns_api_client_provider';

export {
INDEX_PATTERN_ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE,
} from './constants';

0 comments on commit 3cf9144

Please sign in to comment.