Skip to content

Commit

Permalink
Include relations and add prefixLength arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ijreilly committed Sep 2, 2024
1 parent b09aa24 commit 08dab99
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DATABASE_IDENTIFIER_MAXIMUM_LENGTH } from '@/settings/data-model/constants/DatabaseIdentifierMaximumLength';

export const OBJECT_NAME_MAXIMUM_LENGTH = DATABASE_IDENTIFIER_MAXIMUM_LENGTH;
export const OBJECT_NAME_MAXIMUM_LENGTH =
DATABASE_IDENTIFIER_MAXIMUM_LENGTH - 1;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, useFormContext } from 'react-hook-form';
import styled from '@emotion/styled';
import { Controller, useFormContext } from 'react-hook-form';
import { useIcons } from 'twenty-ui';
import { z } from 'zod';

Expand Down Expand Up @@ -164,7 +164,7 @@ export const SettingsDataModelFieldRelationForm = ({
value={value}
onChange={onChange}
fullWidth
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
maxLength={FIELD_NAME_MAXIMUM_LENGTH - 2} // Id suffix
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ObjectMetadataExceptionCode,
} from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { InvalidStringException } from 'src/engine/metadata-modules/utils/exceptions/invalid-string.exception';
import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modules/utils/validate-database-identifier-length.utils';
import { NameTooLongException } from 'src/engine/metadata-modules/utils/exceptions/name-too-long.exception';
import { validateMetadataNameValidityOrThrow } from 'src/engine/metadata-modules/utils/validate-metadata-name-validity.utils';
import { camelCase } from 'src/utils/camel-case';

Expand Down Expand Up @@ -48,14 +48,11 @@ export const validateObjectMetadataInputOrThrow = <
validateNameCamelCasedOrThrow(objectMetadataInput.nameSingular);
validateNameCamelCasedOrThrow(objectMetadataInput.namePlural);

validateNameCharactersOrThrow(objectMetadataInput.nameSingular);
validateNameCharactersOrThrow(objectMetadataInput.namePlural);
validateNameCharactersAndLengthOrThrow(objectMetadataInput.nameSingular);
validateNameCharactersAndLengthOrThrow(objectMetadataInput.namePlural);

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

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

const validateNameIsNotReservedKeywordOrThrow = (name?: string) => {
Expand All @@ -80,24 +77,18 @@ const validateNameCamelCasedOrThrow = (name?: string) => {
}
};

const validateNameIsNotTooLongThrow = (name?: string) => {
if (name) {
if (exceedsDatabaseIdentifierMaximumLength(name)) {
throw new ObjectMetadataException(
`Name exceeds 62 characters: ${name}`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
}
};

const validateNameCharactersOrThrow = (name?: string) => {
const validateNameCharactersAndLengthOrThrow = (name?: string) => {
try {
if (name) {
validateMetadataNameValidityOrThrow(name);
validateMetadataNameValidityOrThrow(name, 1);
}
} catch (error) {
if (error instanceof InvalidStringException) {
if (error instanceof NameTooLongException) {
throw new ObjectMetadataException(
`Name "${name}" is too long`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
} else if (error instanceof InvalidStringException) {
throw new ObjectMetadataException(
`Characters used in name "${name}" are not supported`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
RelationMetadataExceptionCode,
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
import { InvalidStringException } from 'src/engine/metadata-modules/utils/exceptions/invalid-string.exception';
import { NameTooLongException } from 'src/engine/metadata-modules/utils/exceptions/name-too-long.exception';
import { validateFieldNameAvailabilityOrThrow } from 'src/engine/metadata-modules/utils/validate-field-name-availability.utils';
import { validateMetadataNameValidityOrThrow } from 'src/engine/metadata-modules/utils/validate-metadata-name-validity.utils';
import { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.service';
Expand Down Expand Up @@ -62,10 +63,15 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
);

try {
validateMetadataNameValidityOrThrow(relationMetadataInput.fromName);
validateMetadataNameValidityOrThrow(relationMetadataInput.toName);
validateMetadataNameValidityOrThrow(relationMetadataInput.fromName, 2);
validateMetadataNameValidityOrThrow(relationMetadataInput.toName, 2);
} catch (error) {
if (error instanceof InvalidStringException) {
if (error instanceof NameTooLongException) {
throw new RelationMetadataException(
`Name "${relationMetadataInput.fromName}" or "${relationMetadataInput.toName}" is too long`,
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
);
} else if (error instanceof InvalidStringException) {
throw new RelationMetadataException(
`Characters used in name "${relationMetadataInput.fromName}" or "${relationMetadataInput.toName}" are not supported`,
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,34 @@ describe('validateMetadataNameValidityOrThrow', () => {
InvalidStringException,
);
});
it('does not throw if string is less than 63 characters', () => {
const inputWith63Characters =
'thisIsAstringWithSixtyThreeCharacters11111111111111111111111111';
it('does not throw if string is less than 62 characters', () => {
const inputWith62Characters =
'thisIsAstringWithSixtyTwoCharactersiiiiiiiiiiiiiiiiiiiiiiiiiii';

expect(validateMetadataNameValidityOrThrow(inputWith63Characters)).not
expect(validateMetadataNameValidityOrThrow(inputWith62Characters)).not
.toThrow;
});
it('throws error if string is above 63 characters', () => {
const inputWith64Characters =
'thisIsAstringWithSixtyFourCharacters1111111111111111111111111111';
it('throws error if string is 62 characters long with a prefix of length 1', () => {
const inputWith62Characters =
'thisIsAstringWithSixtyTwoCharactersiiiiiiiiiiiiiiiiiiiiiiiiiii';

expect(() =>
validateMetadataNameValidityOrThrow(inputWith62Characters, 1),
).toThrow(NameTooLongException);
});
it('does not throw error if string is 61 characters long with a prefix of length 1', () => {
const inputWith61Characters =
'thisIsAstringWithSixtyOneCharactersiiiiiiiiiiiiiiiiiiiiiiiiii';

expect(validateMetadataNameValidityOrThrow(inputWith61Characters, 1)).not
.toThrow;
});
it('throws error if string is above 62 characters', () => {
const inputWith63Characters =
'thisIsAstringWithSixtyThreeCharactersiiiiiiiiiiiiiiiiiiiiiiiiii';

expect(() =>
validateMetadataNameValidityOrThrow(inputWith64Characters),
validateMetadataNameValidityOrThrow(inputWith63Characters),
).toThrow(NameTooLongException);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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;
export const exceedsDatabaseIdentifierMaximumLength = (
string: string,
prefixLength = 0,
) => {
return string.length > IDENTIFIER_MAX_CHAR_LENGTH - prefixLength;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { exceedsDatabaseIdentifierMaximumLength } from 'src/engine/metadata-modu

const VALID_STRING_PATTERN = /^[a-z][a-zA-Z0-9]*$/;

export const validateMetadataNameValidityOrThrow = (name: string) => {
export const validateMetadataNameValidityOrThrow = (
name: string,
prefixLength = 0,
) => {
if (!name.match(VALID_STRING_PATTERN)) {
throw new InvalidStringException(name);
}
if (exceedsDatabaseIdentifierMaximumLength(name)) {
if (exceedsDatabaseIdentifierMaximumLength(name, prefixLength)) {
throw new NameTooLongException(name);
}
};

0 comments on commit 08dab99

Please sign in to comment.