Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/utils/src/DbNameValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const TABLE_PREFIX = 'table_';
const COLUMN_PREFIX = 'column_';

const VALID_TABLE_NAME = '$+@abc-123_ABC';
const INVALID_TABLE_NAME = '%^&ab-c';
const CLEANED_INVALID_TABLE_NAME = 'ab-c';
const INVALID_TABLE_NAMES = ['%^&ab-c', '-abc', '-'];
const CLEANED_INVALID_TABLE_NAMES = ['ab-c', '-abc', '-'];
Comment thread
vbabich marked this conversation as resolved.

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

it('Returns false on invalid table names', () => {
expect(DbNameValidator.isValidTableName(INVALID_TABLE_NAME)).toBe(false);
});
it.each(INVALID_TABLE_NAMES)(
'Returns false on invalid table name %s',
name => {
expect(DbNameValidator.isValidTableName(name)).toBe(false);
}
);
});

describe('Column name validation', () => {
Expand Down Expand Up @@ -52,10 +55,10 @@ describe('legalizeTableName', () => {
);
});

it('Legalize an invalid table name', () => {
expect(DbNameValidator.legalizeTableName(INVALID_TABLE_NAME)).toBe(
CLEANED_INVALID_TABLE_NAME
);
it.each(
INVALID_TABLE_NAMES.map((name, i) => [name, CLEANED_INVALID_TABLE_NAMES[i]])
)('Legalize an invalid table name %s > %s', (invalid, cleaned) => {
expect(DbNameValidator.legalizeTableName(invalid)).toBe(cleaned);
});

it('Renames a table name with no valid chars to table_0', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/DbNameValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const STERILE_TABLE_AND_NAMESPACE_REGEX = /[^a-zA-Z0-9_$\-+@]/g;
// From io.deephaven.db.tables.utils.DBNameValidator#STERILE_COLUMN_AND_QUERY_REGEX
const STERILE_COLUMN_AND_QUERY_REGEX = /[^A-Za-z0-9_$]/g;

// From io.deephaven.db.tables.utils.DBNameValidator#TABLE_NAME_PATTERN
const TABLE_NAME_PATTERN = /^[a-zA-Z_$][a-zA-Z0-9_$\-+@]*$/g;

function columnNameReplacer(input: string): string {
// Replace all dashes and spaces with underscores
return input.replace(/[ -]/g, '_');
Expand Down Expand Up @@ -155,7 +158,7 @@ class DbNameValidator {
);

static isValidTableName = (name: string): boolean =>
DbNameValidator.legalizeTableName(name) === name;
TABLE_NAME_PATTERN.test(name);

static isValidColumnName = (name: string): boolean =>
DbNameValidator.legalizeColumnName(name) === name;
Expand Down
Loading