Skip to content

Commit 4b1c9dd

Browse files
authored
fix: DH-17599: Fix table name validation - allow hyphens (#2398)
- Updated `STERILE_TABLE_AND_NAMESPACE_REGEX` in `DbNameValidator` to match the Java class and allow hyphens in table names - Added `replace` arg to `DbNameValidator.legalize` to preserve the existing `legalizeColumnName` behavior - replace spaces and hyphens in column names with underscores. - Updated unit tests
1 parent f6a859d commit 4b1c9dd

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

packages/utils/src/DbNameValidator.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import DbNameValidator from './DbNameValidator';
33
const TABLE_PREFIX = 'table_';
44
const COLUMN_PREFIX = 'column_';
55

6-
const VALID_TABLE_NAME = '$+@abc123_ABC';
7-
const INVALID_TABLE_NAME = '%^&abc';
8-
const CLEANED_INVALID_TABLE_NAME = 'abc';
6+
const VALID_TABLE_NAME = '$+@abc-123_ABC';
7+
const INVALID_TABLE_NAME = '%^&ab-c';
8+
const CLEANED_INVALID_TABLE_NAME = 'ab-c';
99

1010
const VALID_COL_NAME = 'abc123_ABC';
11-
const INVALID_COL_NAME = '@abc123_ABC123';
12-
const CLEANED_INVALID_COL_NAME = 'abc123_ABC123';
11+
const INVALID_COL_NAME = '@abc123_ABC-123';
12+
const CLEANED_INVALID_COL_NAME = 'abc123_ABC_123';
1313

1414
const START_WITH_NUM = '123abc';
1515
const RESERVED_JAVA_WORD = 'return';

packages/utils/src/DbNameValidator.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,35 @@ const JAVA_KEYWORDS = new Set([
6363
]);
6464

6565
// From io.deephaven.db.tables.utils.DBNameValidator#STERILE_TABLE_AND_NAMESPACE_REGEX
66-
const STERILE_TABLE_AND_NAMESPACE_REGEX = /[^a-zA-Z0-9_$+@-]/g;
66+
const STERILE_TABLE_AND_NAMESPACE_REGEX = /[^a-zA-Z0-9_$\-+@]/g;
6767

6868
// From io.deephaven.db.tables.utils.DBNameValidator#STERILE_COLUMN_AND_QUERY_REGEX
6969
const STERILE_COLUMN_AND_QUERY_REGEX = /[^A-Za-z0-9_$]/g;
7070

71+
function columnNameReplacer(input: string): string {
72+
// Replace all dashes and spaces with underscores
73+
return input.replace(/[ -]/g, '_');
74+
}
75+
76+
function tableNameReplacer(input: string): string {
77+
// Replace spaces with underscores
78+
return input.replace(/\s/g, '_');
79+
}
80+
7181
/**
7282
* Similar to DBNameValidator.java, this class has utilities for validating and legalizing
7383
* Table and Column names.
7484
*/
7585
class DbNameValidator {
7686
static legalize = (
7787
name: string,
88+
replace: (input: string) => string,
7889
prefix: string,
7990
regex: RegExp,
8091
checkReserved: boolean,
8192
i: number
8293
): string => {
83-
// Replace all dashes and spaces with underscores
84-
let legalName = name.trim().replace(/[- ]/g, '_');
94+
let legalName = replace(name.trim());
8595

8696
// Add prefix to reserved names
8797
if (
@@ -111,6 +121,7 @@ class DbNameValidator {
111121
static legalizeTableName = (name: string): string =>
112122
DbNameValidator.legalize(
113123
name,
124+
tableNameReplacer,
114125
TABLE_PREFIX,
115126
STERILE_TABLE_AND_NAMESPACE_REGEX,
116127
false,
@@ -136,6 +147,7 @@ class DbNameValidator {
136147
// Replace all dashes and spaces with underscores
137148
DbNameValidator.legalize(
138149
header,
150+
columnNameReplacer,
139151
COLUMN_PREFIX,
140152
STERILE_COLUMN_AND_QUERY_REGEX,
141153
true,

0 commit comments

Comments
 (0)