Skip to content

Commit

Permalink
[Automatic Import] Reuse regex pattern to validate names (elastic#205029
Browse files Browse the repository at this point in the history
)

## Summary

This PR reuses the regex pattern to validate the names in UI and backend
created in elastic#204943

---------

Co-authored-by: Ilya Nikokoshev <[email protected]>
  • Loading branch information
bhapas and ilyannn authored Dec 27, 2024
1 parent e4938cb commit b4417c6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
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

0 comments on commit b4417c6

Please sign in to comment.