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

[Automatic Import] Reuse regex pattern to validate names #205029

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ export const CATEGORIZATION_INITIAL_BATCH_SIZE = 60;
export const CATEROGIZATION_REVIEW_BATCH_SIZE = 40;
export const CATEGORIZATION_REVIEW_MAX_CYCLES = 5;
export const CATEGORIZATION_RECURSION_LIMIT = 50;

// Name regex pattern
export const NAME_REGEX_PATTERN = /^[a-z0-9_]+$/;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EuiPanel,
} from '@elastic/eui';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { NAME_REGEX_PATTERN } from '../../../../../../common/constants';
import type { InputType } from '../../../../../../common';
import { useActions, type State } from '../../state';
import type { IntegrationSettings } from '../../types';
Expand Down Expand Up @@ -43,7 +44,7 @@ export const InputTypeOptions: Array<EuiComboBoxOptionOption<InputType>> = [
{ value: 'udp', label: 'UDP' },
];

const isValidName = (name: string) => /^[a-z0-9_]+$/.test(name);
const isValidName = (name: string) => NAME_REGEX_PATTERN.test(name);
const getNameFromTitle = (title: string) => title.toLowerCase().replaceAll(/[^a-z0-9]/g, '_');

interface DataStreamStepProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ describe('renderPackageManifestYAML', () => {

describe('isValidName', () => {
it('should return true for valid names', () => {
expect(isValidName('validName')).toBe(true);
expect(isValidName('Valid_Name')).toBe(true);
expect(isValidName('anotherValidName')).toBe(true);
expect(isValidName('validname')).toBe(true);
expect(isValidName('valid_name')).toBe(true);
expect(isValidName('anothervalidname')).toBe(true);
});

it('should return false for empty string', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import nunjucks from 'nunjucks';
import { getDataPath } from '@kbn/utils';
import { join as joinPath } from 'path';
import { dump } from 'js-yaml';
import { NAME_REGEX_PATTERN } from '../../common/constants';
import type { DataStream, Integration } from '../../common';
import { createSync, ensureDirSync, generateUniqueId, removeDirSync } from '../util';
import { createAgentInput } from './agent';
Expand Down Expand Up @@ -77,8 +78,7 @@ export async function buildPackage(integration: Integration): Promise<Buffer> {
return zipBuffer;
}
export function isValidName(input: string): boolean {
const regex = /^[a-zA-Z0-9_]+$/;
return input.length > 0 && regex.test(input);
return input.length > 0 && NAME_REGEX_PATTERN.test(input);
}
function createDirectories(
workingDir: string,
Expand Down