Skip to content

Commit

Permalink
Forbid names above 63 characters to comply with pg identifier limit (t…
Browse files Browse the repository at this point in the history
…wentyhq#6095)

Fixes twentyhq#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>
  • Loading branch information
ijreilly and charlesBochet authored Jul 4, 2024
1 parent 5df0ea6 commit 6cd154a
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DATABASE_IDENTIFIER_MAXIMUM_LENGTH = 63;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SettingsDataModelFieldAboutFormValues = z.infer<
type SettingsDataModelFieldAboutFormProps = {
disabled?: boolean;
fieldMetadataItem?: FieldMetadataItem;
maxLength?: number;
};

const StyledInputsContainer = styled.div`
Expand All @@ -34,6 +35,7 @@ const StyledInputsContainer = styled.div`
export const SettingsDataModelFieldAboutForm = ({
disabled,
fieldMetadataItem,
maxLength,
}: SettingsDataModelFieldAboutFormProps) => {
const { control } = useFormContext<SettingsDataModelFieldAboutFormValues>();

Expand Down Expand Up @@ -63,6 +65,7 @@ export const SettingsDataModelFieldAboutForm = ({
value={value}
onChange={onChange}
disabled={disabled}
maxLength={maxLength}
fullWidth
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilte
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';
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 { useRelationSettingsFormInitialValues } from '@/settings/data-model/fields/forms/relation/hooks/useRelationSettingsFormInitialValues';
import { RelationType } from '@/settings/data-model/types/RelationType';
Expand Down Expand Up @@ -163,6 +164,7 @@ export const SettingsDataModelFieldRelationForm = ({
value={value}
onChange={onChange}
fullWidth
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { v4 } from 'uuid';

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 { LightIconButton } from '@/ui/input/button/components/LightIconButton';
import { TextInput } from '@/ui/input/components/TextInput';
Expand Down Expand Up @@ -133,6 +134,7 @@ export const SettingsDataModelFieldSelectFormOptionRow = ({
})
}
RightIcon={isDefault ? IconCheck : undefined}
maxLength={OPTION_VALUE_MAXIMUM_LENGTH}
/>
<Dropdown
dropdownId={dropdownIds.actions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { z } from 'zod';

import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
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 { TextArea } from '@/ui/input/components/TextArea';
import { TextInput } from '@/ui/input/components/TextInput';
Expand Down Expand Up @@ -97,6 +98,7 @@ export const SettingsDataModelObjectAboutForm = ({
onChange={onChange}
disabled={disabled || disableNameEdit}
fullWidth
maxLength={OBJECT_NAME_MAXIMUM_LENGTH}
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const TextInputV2Component = (
RightIcon,
LeftIcon,
autoComplete,
maxLength,
}: TextInputV2ComponentProps,
// eslint-disable-next-line @nx/workspace-component-props-naming
ref: ForwardedRef<HTMLInputElement>,
Expand Down Expand Up @@ -182,7 +183,15 @@ const TextInputV2Component = (
onChange?.(event.target.value);
}}
onKeyDown={onKeyDown}
{...{ autoFocus, disabled, placeholder, required, value, LeftIcon }}
{...{
autoFocus,
disabled,
placeholder,
required,
value,
LeftIcon,
maxLength,
}}
/>
<StyledTrailingIconContainer>
{error && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
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 { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard';
import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect';
Expand Down Expand Up @@ -202,6 +203,7 @@ export const SettingsObjectFieldEdit = () => {
<SettingsDataModelFieldAboutForm
disabled={!activeMetadataField.isCustom}
fieldMetadataItem={activeMetadataField}
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/>
</Section>
<Section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
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 { SettingsDataModelFieldSettingsFormCard } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldSettingsFormCard';
import { SettingsDataModelFieldTypeSelect } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect';
Expand Down Expand Up @@ -202,7 +203,9 @@ export const SettingsObjectNewFieldStep2 = () => {
title="Name and description"
description="The name and description of this field"
/>
<SettingsDataModelFieldAboutForm />
<SettingsDataModelFieldAboutForm
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
/>
</Section>
<Section>
<H2Title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { assertMutationNotOnRemoteObject } from 'src/engine/metadata-modules/object-metadata/utils/assert-mutation-not-on-remote-object.util';
import {
validateMetadataName,
validateMetadataNameOrThrow,
InvalidStringException,
NameTooLongException,
} 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 {
FieldMetadataException,
FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';

import {
FieldMetadataEntity,
Expand Down Expand Up @@ -601,19 +603,35 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
>(fieldMetadataInput: T): T {
if (fieldMetadataInput.name) {
try {
validateMetadataName(fieldMetadataInput.name);
validateMetadataNameOrThrow(fieldMetadataInput.name);
} catch (error) {
if (error instanceof InvalidStringException) {
throw new FieldMetadataException(
`Characters used in name "${fieldMetadataInput.name}" are not supported`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
} else if (error instanceof NameTooLongException) {
throw new FieldMetadataException(
`Name "${fieldMetadataInput.name}" exceeds 63 characters`,
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
} else {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
ObjectMetadataException,
ObjectMetadataExceptionCode,
} from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';
import {
validateMetadataName,
validateMetadataNameOrThrow,
InvalidStringException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
import { camelCase } from 'src/utils/camel-case';
Expand Down Expand Up @@ -54,6 +55,9 @@ export const validateObjectMetadataInputOrThrow = <

validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.nameSingular);
validateNameIsNotReservedKeywordOrThrow(objectMetadataInput.namePlural);

validateNameIsNotTooLongThrow(objectMetadataInput.nameSingular);
validateNameIsNotTooLongThrow(objectMetadataInput.namePlural);
};

const validateNameIsNotReservedKeywordOrThrow = (name?: string) => {
Expand All @@ -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) => {
try {
if (name) {
validateMetadataName(name);
validateMetadataNameOrThrow(name);
}
} catch (error) {
if (error instanceof InvalidStringException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util';
import {
validateMetadataName,
validateMetadataNameOrThrow,
InvalidStringException,
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
Expand Down Expand Up @@ -63,8 +63,8 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
);

try {
validateMetadataName(relationMetadataInput.fromName);
validateMetadataName(relationMetadataInput.toName);
validateMetadataNameOrThrow(relationMetadataInput.fromName);
validateMetadataNameOrThrow(relationMetadataInput.toName);
} catch (error) {
if (error instanceof InvalidStringException) {
throw new RelationMetadataException(
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const IDENTIFIER_MAX_CHAR_LENGTH = 63;
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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);
}
};

Expand All @@ -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);
}
}

0 comments on commit 6cd154a

Please sign in to comment.