From 64b3fa3d1771a69d62814977d3ac3782a35aeb35 Mon Sep 17 00:00:00 2001 From: Yiorgis Gozadinos Date: Fri, 16 May 2025 09:38:21 +0200 Subject: [PATCH] fix(core): Allow strings starting with numbers in alphanumeric string validator (#15425) --- packages/workflow/src/TypeValidation.ts | 2 ++ packages/workflow/test/TypeValidation.test.ts | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/workflow/src/TypeValidation.ts b/packages/workflow/src/TypeValidation.ts index 446ee3f619e..845c5dc3e87 100644 --- a/packages/workflow/src/TypeValidation.ts +++ b/packages/workflow/src/TypeValidation.ts @@ -35,6 +35,8 @@ export const tryToParseString = (value: unknown): string => { }; export const tryToParseAlphanumericString = (value: unknown): string => { const parsed = tryToParseString(value); + // We do not allow special characters, only letters, numbers and underscore + // Numbers not allowed as the first character const regex = /^[a-zA-Z_][a-zA-Z0-9_]*$/; if (!regex.test(parsed)) { throw new ApplicationError('Value is not a valid alphanumeric string', { extra: { value } }); diff --git a/packages/workflow/test/TypeValidation.test.ts b/packages/workflow/test/TypeValidation.test.ts index 2913bb553d8..17f52695810 100644 --- a/packages/workflow/test/TypeValidation.test.ts +++ b/packages/workflow/test/TypeValidation.test.ts @@ -3,6 +3,38 @@ import { DateTime, Settings } from 'luxon'; import { getValueDescription, tryToParseDateTime, validateFieldType } from '@/TypeValidation'; describe('Type Validation', () => { + describe('string-alphanumeric', () => { + test('should validate and parse alphanumeric strings, not starting with a number', () => { + const VALID_STRINGS = ['abc123', 'ABC123', 'abc_123', '_abc123', 'abcABC123_']; + VALID_STRINGS.forEach((value) => + expect(validateFieldType('string', value, 'string-alphanumeric')).toEqual({ + valid: true, + newValue: value, + }), + ); + }); + + test('should not validate non-alphanumeric strings, or starting with a number', () => { + const INVALID_STRINGS = [ + 'abc-123', + 'abc 123', + 'abc@123', + 'abc#123', + 'abc.123', + 'abc$123', + 'abc&123', + 'abc!123', + 'abc(123)', + 'bπc123', + 'πι', + '123abc', // Cannot start with number + '456_abc', // Cannot start with number + ]; + INVALID_STRINGS.forEach((value) => + expect(validateFieldType('string', value, 'string-alphanumeric').valid).toBe(false), + ); + }); + }); describe('Dates', () => { test('should validate and cast ISO dates', () => { const VALID_ISO_DATES = [