Skip to content

Commit 2055bc9

Browse files
vbabichCopilot
andauthored
fix: Cherry-pick DH-17599: Fix table name validation (#2403) (#2404)
PR #2398 allowed hyphens in namespace and table names. However, I noticed that server-side validation rejects names starting with a hyphen, while these names appear valid on the client-side. `io.deephaven.db.tables.utils.DBNameValidator` uses separate regular expressions to sanitize and validate names. This change adds the correct `TABLE_NAME_PATTERN` for table name validation instead of checking if the name matches its sanitized version. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d7dec90 commit 2055bc9

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

packages/utils/src/DbNameValidator.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const TABLE_PREFIX = 'table_';
44
const COLUMN_PREFIX = 'column_';
55

66
const VALID_TABLE_NAME = '$+@abc-123_ABC';
7-
const INVALID_TABLE_NAME = '%^&ab-c';
8-
const CLEANED_INVALID_TABLE_NAME = 'ab-c';
7+
const INVALID_TABLE_NAMES = ['%^&ab-c', '-abc', '-'];
8+
const CLEANED_INVALID_TABLE_NAMES = ['ab-c', '-abc', '-'];
99

1010
const VALID_COL_NAME = 'abc123_ABC';
1111
const INVALID_COL_NAME = '@abc123_ABC-123';
@@ -20,9 +20,12 @@ describe('Table name validation', () => {
2020
expect(DbNameValidator.isValidTableName(VALID_TABLE_NAME)).toBe(true);
2121
});
2222

23-
it('Returns false on invalid table names', () => {
24-
expect(DbNameValidator.isValidTableName(INVALID_TABLE_NAME)).toBe(false);
25-
});
23+
it.each(INVALID_TABLE_NAMES)(
24+
'Returns false on invalid table name %s',
25+
name => {
26+
expect(DbNameValidator.isValidTableName(name)).toBe(false);
27+
}
28+
);
2629
});
2730

2831
describe('Column name validation', () => {
@@ -52,10 +55,10 @@ describe('legalizeTableName', () => {
5255
);
5356
});
5457

55-
it('Legalize an invalid table name', () => {
56-
expect(DbNameValidator.legalizeTableName(INVALID_TABLE_NAME)).toBe(
57-
CLEANED_INVALID_TABLE_NAME
58-
);
58+
it.each(
59+
INVALID_TABLE_NAMES.map((name, i) => [name, CLEANED_INVALID_TABLE_NAMES[i]])
60+
)('Legalize an invalid table name %s > %s', (invalid, cleaned) => {
61+
expect(DbNameValidator.legalizeTableName(invalid)).toBe(cleaned);
5962
});
6063

6164
it('Renames a table name with no valid chars to table_0', () => {

packages/utils/src/DbNameValidator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const STERILE_TABLE_AND_NAMESPACE_REGEX = /[^a-zA-Z0-9_$\-+@]/g;
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+
// From io.deephaven.db.tables.utils.DBNameValidator#TABLE_NAME_PATTERN
72+
const TABLE_NAME_PATTERN = /^[a-zA-Z_$][a-zA-Z0-9_$\-+@]*$/g;
73+
7174
function columnNameReplacer(input: string): string {
7275
// Replace all dashes and spaces with underscores
7376
return input.replace(/[ -]/g, '_');
@@ -155,7 +158,7 @@ class DbNameValidator {
155158
);
156159

157160
static isValidTableName = (name: string): boolean =>
158-
DbNameValidator.legalizeTableName(name) === name;
161+
TABLE_NAME_PATTERN.test(name);
159162

160163
static isValidColumnName = (name: string): boolean =>
161164
DbNameValidator.legalizeColumnName(name) === name;

0 commit comments

Comments
 (0)