[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 <ilya.nikokoshev@elastic.co>
This commit is contained in:
Bharat Pasupula 2024-12-27 18:39:10 +01:00 committed by GitHub
parent e4938cb13b
commit b4417c6957
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 10 additions and 6 deletions

View file

@ -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_]+$/;

View file

@ -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<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 {

View file

@ -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', () => {

View file

@ -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<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,