fix(core): Allow strings starting with numbers in alphanumeric string validator (#15425)

This commit is contained in:
Yiorgis Gozadinos
2025-05-16 09:38:21 +02:00
committed by GitHub
parent ec63a61652
commit 64b3fa3d17
2 changed files with 34 additions and 0 deletions

View File

@@ -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 } });

View File

@@ -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 = [