From f4c1d270a3389d67be12bf826903b1e7037a96e5 Mon Sep 17 00:00:00 2001 From: Bharat Pasupula <123897612+bhapas@users.noreply.github.com> Date: Fri, 27 Dec 2024 18:39:10 +0100 Subject: [PATCH] [Automatic Import] Reuse regex pattern to validate names (#205029) ## Summary This PR reuses the regex pattern to validate the names in UI and backend created in https://github.com/elastic/kibana/pull/204943 --------- Co-authored-by: Ilya Nikokoshev (cherry picked from commit b4417c695722eadc14a6304171ca2d9ec90064bb) # Conflicts: # x-pack/plugins/integration_assistant/server/integration_builder/build_integration.ts --- x-pack/plugins/integration_assistant/common/constants.ts | 3 +++ .../steps/data_stream_step/data_stream_step.tsx | 3 ++- .../server/integration_builder/build_integration.test.ts | 6 +++--- .../server/integration_builder/build_integration.ts | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/integration_assistant/common/constants.ts b/x-pack/plugins/integration_assistant/common/constants.ts index 3026101ebf54d..93340af59eee3 100644 --- a/x-pack/plugins/integration_assistant/common/constants.ts +++ b/x-pack/plugins/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/plugins/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx b/x-pack/plugins/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx index 4b505fb7062d6..a9092deeadd8f 100644 --- a/x-pack/plugins/integration_assistant/public/components/create_integration/create_integration_assistant/steps/data_stream_step/data_stream_step.tsx +++ b/x-pack/plugins/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/plugins/integration_assistant/server/integration_builder/build_integration.test.ts b/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.test.ts index a99eaba6446cc..b3d722ced0101 100644 --- a/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.test.ts +++ b/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.test.ts @@ -282,9 +282,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', () => { diff --git a/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.ts b/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.ts index 22ef37b8f90b1..80bb804bf874b 100644 --- a/x-pack/plugins/integration_assistant/server/integration_builder/build_integration.ts +++ b/x-pack/plugins/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 { safeDump } 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'; @@ -76,8 +77,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,