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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 127 additions and 19 deletions

View File

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

View File

@ -0,0 +1,3 @@
import { DATABASE_IDENTIFIER_MAXIMUM_LENGTH } from '@/settings/data-model/constants/DatabaseIdentifierMaximumLength';
export const FIELD_NAME_MAXIMUM_LENGTH = DATABASE_IDENTIFIER_MAXIMUM_LENGTH;

View File

@ -0,0 +1,3 @@
import { DATABASE_IDENTIFIER_MAXIMUM_LENGTH } from '@/settings/data-model/constants/DatabaseIdentifierMaximumLength';
export const OBJECT_NAME_MAXIMUM_LENGTH = DATABASE_IDENTIFIER_MAXIMUM_LENGTH;

View File

@ -0,0 +1,3 @@
import { DATABASE_IDENTIFIER_MAXIMUM_LENGTH } from '@/settings/data-model/constants/DatabaseIdentifierMaximumLength';
export const OPTION_VALUE_MAXIMUM_LENGTH = DATABASE_IDENTIFIER_MAXIMUM_LENGTH;

View File

@ -22,6 +22,7 @@ type SettingsDataModelFieldAboutFormValues = z.infer<
type SettingsDataModelFieldAboutFormProps = { type SettingsDataModelFieldAboutFormProps = {
disabled?: boolean; disabled?: boolean;
fieldMetadataItem?: FieldMetadataItem; fieldMetadataItem?: FieldMetadataItem;
maxLength?: number;
}; };
const StyledInputsContainer = styled.div` const StyledInputsContainer = styled.div`
@ -34,6 +35,7 @@ const StyledInputsContainer = styled.div`
export const SettingsDataModelFieldAboutForm = ({ export const SettingsDataModelFieldAboutForm = ({
disabled, disabled,
fieldMetadataItem, fieldMetadataItem,
maxLength,
}: SettingsDataModelFieldAboutFormProps) => { }: SettingsDataModelFieldAboutFormProps) => {
const { control } = useFormContext<SettingsDataModelFieldAboutFormValues>(); const { control } = useFormContext<SettingsDataModelFieldAboutFormValues>();
@ -63,6 +65,7 @@ export const SettingsDataModelFieldAboutForm = ({
value={value} value={value}
onChange={onChange} onChange={onChange}
disabled={disabled} disabled={disabled}
maxLength={maxLength}
fullWidth fullWidth
/> />
)} )}

View File

@ -7,6 +7,7 @@ import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilte
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation'; import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema'; import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema';
import { FIELD_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/FieldNameMaximumLength';
import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes'; import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes';
import { useRelationSettingsFormInitialValues } from '@/settings/data-model/fields/forms/relation/hooks/useRelationSettingsFormInitialValues'; import { useRelationSettingsFormInitialValues } from '@/settings/data-model/fields/forms/relation/hooks/useRelationSettingsFormInitialValues';
import { RelationType } from '@/settings/data-model/types/RelationType'; import { RelationType } from '@/settings/data-model/types/RelationType';
@ -163,6 +164,7 @@ export const SettingsDataModelFieldRelationForm = ({
value={value} value={value}
onChange={onChange} onChange={onChange}
fullWidth fullWidth
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/> />
)} )}
/> />

View File

@ -13,6 +13,7 @@ import {
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem'; import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem';
import { OPTION_VALUE_MAXIMUM_LENGTH } from '@/settings/data-model/constants/OptionValueMaximumLength';
import { getOptionValueFromLabel } from '@/settings/data-model/fields/forms/select/utils/getOptionValueFromLabel'; import { getOptionValueFromLabel } from '@/settings/data-model/fields/forms/select/utils/getOptionValueFromLabel';
import { LightIconButton } from '@/ui/input/button/components/LightIconButton'; import { LightIconButton } from '@/ui/input/button/components/LightIconButton';
import { TextInput } from '@/ui/input/components/TextInput'; import { TextInput } from '@/ui/input/components/TextInput';
@ -133,6 +134,7 @@ export const SettingsDataModelFieldSelectFormOptionRow = ({
}) })
} }
RightIcon={isDefault ? IconCheck : undefined} RightIcon={isDefault ? IconCheck : undefined}
maxLength={OPTION_VALUE_MAXIMUM_LENGTH}
/> />
<Dropdown <Dropdown
dropdownId={dropdownIds.actions} dropdownId={dropdownIds.actions}

View File

@ -4,6 +4,7 @@ import { z } from 'zod';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { objectMetadataItemSchema } from '@/object-metadata/validation-schemas/objectMetadataItemSchema'; import { objectMetadataItemSchema } from '@/object-metadata/validation-schemas/objectMetadataItemSchema';
import { OBJECT_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/ObjectNameMaximumLength';
import { IconPicker } from '@/ui/input/components/IconPicker'; import { IconPicker } from '@/ui/input/components/IconPicker';
import { TextArea } from '@/ui/input/components/TextArea'; import { TextArea } from '@/ui/input/components/TextArea';
import { TextInput } from '@/ui/input/components/TextInput'; import { TextInput } from '@/ui/input/components/TextInput';
@ -97,6 +98,7 @@ export const SettingsDataModelObjectAboutForm = ({
onChange={onChange} onChange={onChange}
disabled={disabled || disableNameEdit} disabled={disabled || disableNameEdit}
fullWidth fullWidth
maxLength={OBJECT_NAME_MAXIMUM_LENGTH}
/> />
)} )}
/> />

View File

@ -145,6 +145,7 @@ const TextInputV2Component = (
RightIcon, RightIcon,
LeftIcon, LeftIcon,
autoComplete, autoComplete,
maxLength,
}: TextInputV2ComponentProps, }: TextInputV2ComponentProps,
// eslint-disable-next-line @nx/workspace-component-props-naming // eslint-disable-next-line @nx/workspace-component-props-naming
ref: ForwardedRef<HTMLInputElement>, ref: ForwardedRef<HTMLInputElement>,
@ -182,7 +183,15 @@ const TextInputV2Component = (
onChange?.(event.target.value); onChange?.(event.target.value);
}} }}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
{...{ autoFocus, disabled, placeholder, required, value, LeftIcon }} {...{
autoFocus,
disabled,
placeholder,
required,
value,
LeftIcon,
maxLength,
}}
/> />
<StyledTrailingIconContainer> <StyledTrailingIconContainer>
{error && ( {error && (

View File

@ -22,6 +22,7 @@ import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons'; import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer'; import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer'; import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { FIELD_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/FieldNameMaximumLength';
import { SettingsDataModelFieldAboutForm } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm'; import { SettingsDataModelFieldAboutForm } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm';
import { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard'; import { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard';
import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect'; import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect';
@ -202,6 +203,7 @@ export const SettingsObjectFieldEdit = () => {
<SettingsDataModelFieldAboutForm <SettingsDataModelFieldAboutForm
disabled={!activeMetadataField.isCustom} disabled={!activeMetadataField.isCustom}
fieldMetadataItem={activeMetadataField} fieldMetadataItem={activeMetadataField}
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/> />
</Section> </Section>
<Section> <Section>

View File

@ -17,6 +17,7 @@ import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons'; import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer'; import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer'; import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { FIELD_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/FieldNameMaximumLength';
import { SettingsDataModelFieldAboutForm } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm'; import { SettingsDataModelFieldAboutForm } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm';
import { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard'; import { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard';
import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect'; import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect';
@ -202,7 +203,9 @@ export const SettingsObjectNewFieldStep2 = () => {
title="Name and description" title="Name and description"
description="The name and description of this field" description="The name and description of this field"
/> />
<SettingsDataModelFieldAboutForm /> <SettingsDataModelFieldAboutForm
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/>
</Section> </Section>
<Section> <Section>
<H2Title <H2Title

View File

@ -35,14 +35,16 @@ import { DeleteOneFieldInput } from 'src/engine/metadata-modules/field-metadata/
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util'; import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import { assertMutationNotOnRemoteObject } from 'src/engine/metadata-modules/object-metadata/utils/assert-mutation-not-on-remote-object.util'; import { assertMutationNotOnRemoteObject } from 'src/engine/metadata-modules/object-metadata/utils/assert-mutation-not-on-remote-object.util';
import { import {
validateMetadataName, validateMetadataNameOrThrow,
InvalidStringException, InvalidStringException,
NameTooLongException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service'; import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
import { import {
FieldMetadataException, FieldMetadataException,
FieldMetadataExceptionCode, FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception'; } from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';
import { import {
FieldMetadataEntity, FieldMetadataEntity,
@ -601,19 +603,35 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
>(fieldMetadataInput: T): T { >(fieldMetadataInput: T): T {
if (fieldMetadataInput.name) { if (fieldMetadataInput.name) {
try { try {
validateMetadataName(fieldMetadataInput.name); validateMetadataNameOrThrow(fieldMetadataInput.name);
} catch (error) { } catch (error) {
if (error instanceof InvalidStringException) { if (error instanceof InvalidStringException) {
throw new FieldMetadataException( throw new FieldMetadataException(
`Characters used in name "${fieldMetadataInput.name}" are not supported`, `Characters used in name "${fieldMetadataInput.name}" are not supported`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT, FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
); );
} else if (error instanceof NameTooLongException) {
throw new FieldMetadataException(
`Name "${fieldMetadataInput.name}" exceeds 63 characters`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
} else { } else {
throw error; throw error;
} }
} }
} }
if (fieldMetadataInput.options) {
for (const option of fieldMetadataInput.options) {
if (exceedsDatabaseIdentifierMaximumLength(option.value)) {
throw new FieldMetadataException(
`Option value "${option.value}" exceeds 63 characters`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
}
}
}
return fieldMetadataInput; return fieldMetadataInput;
} }
} }

View File

@ -4,8 +4,9 @@ import {
ObjectMetadataException, ObjectMetadataException,
ObjectMetadataExceptionCode, ObjectMetadataExceptionCode,
} from 'src/engine/metadata-modules/object-metadata/object-metadata.exception'; } from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';
import { import {
validateMetadataName, validateMetadataNameOrThrow,
InvalidStringException, InvalidStringException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
import { camelCase } from 'src/utils/camel-case'; import { camelCase } from 'src/utils/camel-case';
@ -54,6 +55,9 @@ export const validateObjectMetadataInputOrThrow = <
validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.nameSingular); validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.nameSingular);
validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.namePlural); validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.namePlural);
validateNameIsNotTooLongThrow(objectMetadataInput.nameSingular);
validateNameIsNotTooLongThrow(objectMetadataInput.namePlural);
}; };
const validateNameIsNotReservedKeywordOrThrow = (name?: string) => { const validateNameIsNotReservedKeywordOrThrow = (name?: string) => {
@ -78,10 +82,21 @@ const validateNameCamelCasedOrThrow = (name?: string) => {
} }
}; };
const validateNameIsNotTooLongThrow = (name?: string) => {
if (name) {
if (exceedsDatabaseIdentifierMaximumLength(name)) {
throw new ObjectMetadataException(
`Name exceeds 63 characters: ${name}`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
}
};
const validateNameCharactersOrThrow = (name?: string) => { const validateNameCharactersOrThrow = (name?: string) => {
try { try {
if (name) { if (name) {
validateMetadataName(name); validateMetadataNameOrThrow(name);
} }
} catch (error) { } catch (error) {
if (error instanceof InvalidStringException) { if (error instanceof InvalidStringException) {

View File

@ -24,7 +24,7 @@ import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadat
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util'; import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util'; import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util';
import { import {
validateMetadataName, validateMetadataNameOrThrow,
InvalidStringException, InvalidStringException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service'; import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
@ -63,8 +63,8 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
); );
try { try {
validateMetadataName(relationMetadataInput.fromName); validateMetadataNameOrThrow(relationMetadataInput.fromName);
validateMetadataName(relationMetadataInput.toName); validateMetadataNameOrThrow(relationMetadataInput.toName);
} catch (error) { } catch (error) {
if (error instanceof InvalidStringException) { if (error instanceof InvalidStringException) {
throw new RelationMetadataException( throw new RelationMetadataException(

View File

@ -1,34 +1,57 @@
import { import {
validateMetadataName, validateMetadataNameOrThrow,
InvalidStringException, InvalidStringException,
NameTooLongException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
describe('validateMetadataName', () => { describe('validateMetadataNameOrThrow', () => {
it('does not throw if string is valid', () => { it('does not throw if string is valid', () => {
const input = 'testName'; const input = 'testName';
expect(validateMetadataName(input)).not.toThrow; expect(validateMetadataNameOrThrow(input)).not.toThrow;
}); });
it('throws error if string has spaces', () => { it('throws error if string has spaces', () => {
const input = 'name with 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', () => { it('throws error if string starts with capital letter', () => {
const input = 'StringStartingWithCapitalLetter'; const input = 'StringStartingWithCapitalLetter';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException); expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
}); });
it('throws error if string has non latin characters', () => { it('throws error if string has non latin characters', () => {
const input = 'בְרִבְרִ'; const input = 'בְרִבְרִ';
expect(() => validateMetadataName(input)).toThrow(InvalidStringException); expect(() => validateMetadataNameOrThrow(input)).toThrow(
InvalidStringException,
);
}); });
it('throws error if starts with digits', () => { it('throws error if starts with digits', () => {
const input = '123string'; 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]*$/; const VALID_STRING_PATTERN = /^[a-z][a-zA-Z0-9]*$/;
export const validateMetadataName = (string: string) => { export const validateMetadataNameOrThrow = (name: string) => {
if (!string.match(VALID_STRING_PATTERN)) { if (!name.match(VALID_STRING_PATTERN)) {
throw new InvalidStringException(string); throw new InvalidStringException(name);
}
if (exceedsDatabaseIdentifierMaximumLength(name)) {
throw new NameTooLongException(name);
} }
}; };
@ -13,3 +18,11 @@ export class InvalidStringException extends Error {
super(message); super(message);
} }
} }
export class NameTooLongException extends Error {
constructor(string: string) {
const message = `String "${string}" exceeds 63 characters limit`;
super(message);
}
}