From def1774bf06ac69e7e2684af3406ea9bd612c47a Mon Sep 17 00:00:00 2001 From: Marie <51697796+ijreilly@users.noreply.github.com> Date: Sat, 25 May 2024 10:29:00 +0200 Subject: [PATCH] [Fix] Object names should be camel cased (#5571) as per title --- ...alidate-object-metadata-input.util.spec.ts | 22 +++++++++++++++++++ .../validate-object-metadata-input.util.ts | 12 ++++++++++ .../remote-table/remote-table.service.ts | 4 ++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/__tests__/validate-object-metadata-input.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/__tests__/validate-object-metadata-input.util.spec.ts index d2208782aa..24e70b1fd1 100644 --- a/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/__tests__/validate-object-metadata-input.util.spec.ts +++ b/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/__tests__/validate-object-metadata-input.util.spec.ts @@ -60,4 +60,26 @@ describe('validateObjectName', () => { validateObjectMetadataInputOrThrow(invalidObjectInput), ).toThrow(); }); + + it('should throw if nameSingular is not camelCased', async () => { + const invalidObjectInput = { + ...validObjectInput, + nameSingular: 'notACamelCase1a', + }; + + expect(() => + validateObjectMetadataInputOrThrow(invalidObjectInput), + ).toThrow(); + }); + + it('should throw if namePlural is a not camelCased', async () => { + const invalidObjectInput = { + ...validObjectInput, + namePlural: 'notACamelCase1b', + }; + + expect(() => + validateObjectMetadataInputOrThrow(invalidObjectInput), + ).toThrow(); + }); }); diff --git a/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util.ts b/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util.ts index 2a9efe39b7..42d1c1b005 100644 --- a/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util.ts +++ b/packages/twenty-server/src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util.ts @@ -4,6 +4,7 @@ import { InvalidStringException } from 'src/engine/metadata-modules/errors/Inval import { CreateObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/create-object.input'; import { UpdateObjectPayload } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input'; import { validateMetadataName } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; +import { camelCase } from 'src/utils/camel-case'; const coreObjectNames = [ 'appToken', @@ -41,6 +42,9 @@ export const validateObjectMetadataInputOrThrow = < >( objectMetadataInput: T, ): void => { + validateNameCamelCasedOrThrow(objectMetadataInput.nameSingular); + validateNameCamelCasedOrThrow(objectMetadataInput.namePlural); + validateNameCharactersOrThrow(objectMetadataInput.nameSingular); validateNameCharactersOrThrow(objectMetadataInput.namePlural); @@ -56,6 +60,14 @@ const validateNameIsNotReservedKeywordOrThrow = (name?: string) => { } }; +const validateNameCamelCasedOrThrow = (name?: string) => { + if (name) { + if (name !== camelCase(name)) { + throw new ForbiddenException(`Name should be in camelCase: ${name}`); + } + } +}; + const validateNameCharactersOrThrow = (name?: string) => { try { if (name) { diff --git a/packages/twenty-server/src/engine/metadata-modules/remote-server/remote-table/remote-table.service.ts b/packages/twenty-server/src/engine/metadata-modules/remote-server/remote-table/remote-table.service.ts index 0d7f04cedb..e05f5accd5 100644 --- a/packages/twenty-server/src/engine/metadata-modules/remote-server/remote-table/remote-table.service.ts +++ b/packages/twenty-server/src/engine/metadata-modules/remote-server/remote-table/remote-table.service.ts @@ -416,8 +416,8 @@ export class RemoteTableService { : plural(localTableBaseName); const objectMetadata = await this.objectMetadataService.createOne({ - nameSingular: localTableNameSingular, - namePlural: localTableNamePlural, + nameSingular: camelCase(localTableNameSingular), + namePlural: camelCase(localTableNamePlural), labelSingular: camelToTitleCase(camelCase(localTableBaseName)), labelPlural: camelToTitleCase(plural(camelCase(localTableBaseName))), description: 'Remote table',