From 8b34d2781adeb1718d515e30862faf1b98de7397 Mon Sep 17 00:00:00 2001 From: Bharat Pasupula Date: Fri, 20 Dec 2024 06:29:50 +0100 Subject: [PATCH 1/2] reuse regex pattern --- .../plugins/shared/integration_assistant/common/constants.ts | 3 +++ .../steps/data_stream_step/data_stream_step.tsx | 3 ++- .../server/integration_builder/build_integration.ts | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x-pack/platform/plugins/shared/integration_assistant/common/constants.ts b/x-pack/platform/plugins/shared/integration_assistant/common/constants.ts index 2e5975a1d4074..e05692f4ac085 100644 --- a/x-pack/platform/plugins/shared/integration_assistant/common/constants.ts +++ b/x-pack/platform/plugins/shared/integration_assistant/common/constants.ts @@ -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_]+$/; diff --git a/x-pack/platform/plugins/shared/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx b/x-pack/platform/plugins/shared/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx index 1bd7b92b2e0cb..528df2af2a156 100644 --- a/x-pack/platform/plugins/shared/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx +++ b/x-pack/platform/plugins/shared/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx @@ -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'; @@ -43,7 +44,7 @@ export const InputTypeOptions: Array> = [ { 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 { diff --git a/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.ts b/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.ts index 959f1ef093832..9cdf4c7234b0f 100644 --- a/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.ts +++ b/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.ts @@ -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'; @@ -77,8 +78,7 @@ export async function buildPackage(integration: Integration): Promise { 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, From 772b0edea63d7c12cf33615920cb9184c35d07af Mon Sep 17 00:00:00 2001 From: Bharat Pasupula Date: Sun, 22 Dec 2024 07:19:43 +0100 Subject: [PATCH 2/2] fix test --- .../server/integration_builder/build_integration.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.test.ts b/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.test.ts index 294105a1621af..23d7dcce24649 100644 --- a/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.test.ts +++ b/x-pack/platform/plugins/shared/integration_assistant/server/integration_builder/build_integration.test.ts @@ -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', () => {