Forbid names above 63 characters to comply with pg identifier limit (#6095)

Fixes #6032.

Pg has a char limit on identifiers (= table, columns, enum names) of 63
bytes.
Let's limit the metadata names that will be converted to identifiers
(objects names, fields names, relation names, enum values) to 63 chars.
For the sake of simplicity in the FE we will limit the input length of
labels.

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Marie
2024-07-04 15:32:42 +02:00
committed by GitHub
parent 5df0ea6466
commit 6cd154aac6
18 changed files with 127 additions and 19 deletions

View File

@ -1,34 +1,57 @@
import {
validateMetadataName,
validateMetadataNameOrThrow,
InvalidStringException,
NameTooLongException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
describe('validateMetadataName', () => {
describe('validateMetadataNameOrThrow', () => {
it('does not throw if string is valid', () => {
const input = 'testName';
expect(validateMetadataName(input)).not.toThrow;
expect(validateMetadataNameOrThrow(input)).not.toThrow;
});
it('throws error if string has spaces', () => {
const input = 'name with spaces';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException);
expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
});
it('throws error if string starts with capital letter', () => {
const input = 'StringStartingWithCapitalLetter';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException);
expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
});
it('throws error if string has non latin characters', () => {
const input = 'בְרִבְרִ';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException);
expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
});
it('throws error if starts with digits', () => {
const input = '123string';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException);
expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
});
it('does not throw if string is less than 63 characters', () => {
const inputWith63Characters =
'thisIsAstringWithSixtyThreeCharacters11111111111111111111111111';
expect(validateMetadataNameOrThrow(inputWith63Characters)).not.toThrow;
});
it('throws error if string is above 63 characters', () => {
const inputWith64Characters =
'thisIsAstringWithSixtyFourCharacters1111111111111111111111111111';
expect(() => validateMetadataNameOrThrow(inputWith64Characters)).toThrow(
NameTooLongException,
);
});
});

View File

@ -0,0 +1 @@
export const IDENTIFIER_MAX_CHAR_LENGTH = 63;

View File

@ -0,0 +1,5 @@
import { IDENTIFIER_MAX_CHAR_LENGTH } from 'src/engine/metadata-modules/utils/metadata.constants';
export const exceedsDatabaseIdentifierMaximumLength = (string: string) => {
return string.length > IDENTIFIER_MAX_CHAR_LENGTH;
};

View File

@ -1,8 +1,13 @@
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';
const VALID_STRING_PATTERN = /^[a-z][a-zA-Z0-9]*$/;
export const validateMetadataName = (string: string) => {
if (!string.match(VALID_STRING_PATTERN)) {
throw new InvalidStringException(string);
export const validateMetadataNameOrThrow = (name: string) => {
if (!name.match(VALID_STRING_PATTERN)) {
throw new InvalidStringException(name);
}
if (exceedsDatabaseIdentifierMaximumLength(name)) {
throw new NameTooLongException(name);
}
};
@ -13,3 +18,11 @@ export class InvalidStringException extends Error {
super(message);
}
}
export class NameTooLongException extends Error {
constructor(string: string) {
const message = `String "${string}" exceeds 63 characters limit`;
super(message);
}
}