forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] Support non latin characters in schema names (twentyhq#5063)
Fixes twentyhq#4943 ## How was it tested? Local (front + /metadata) Unit tests for utils --------- Co-authored-by: Weiko <[email protected]>
- Loading branch information
Showing
16 changed files
with
236 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,8 @@ | |
}, | ||
"msw": { | ||
"workerDirectory": "public" | ||
}, | ||
"dependencies": { | ||
"transliteration": "^2.3.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/twenty-front/src/modules/object-metadata/utils/validateMetadataLabel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const metadataLabelValidationPattern = /^[a-zA-Z][a-zA-Z0-9 ]*$/; | ||
const metadataLabelValidationPattern = /^[^0-9].*$/; | ||
|
||
export const validateMetadataLabel = (value: string) => | ||
!!value.match(metadataLabelValidationPattern); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...front/src/pages/settings/data-model/utils/__tests__/format-metadata-label-to-name.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { formatMetadataLabelToMetadataNameOrThrows } from '~/pages/settings/data-model/utils/format-metadata-label-to-name.util'; | ||
|
||
const VALID_STRING_PATTERN = /^[a-zA-Z][a-zA-Z0-9 ]*$/; | ||
|
||
describe('formatMetadataLabelToMetadataNameOrThrows', () => { | ||
it('leaves strings unchanged if only latin characters', () => { | ||
const input = 'testName'; | ||
|
||
expect( | ||
formatMetadataLabelToMetadataNameOrThrows(input).match( | ||
VALID_STRING_PATTERN, | ||
)?.length, | ||
).toBe(1); | ||
expect(formatMetadataLabelToMetadataNameOrThrows(input)).toEqual(input); | ||
}); | ||
|
||
it('leaves strings unchanged if only latin characters and digits', () => { | ||
const input = 'testName123'; | ||
|
||
expect( | ||
formatMetadataLabelToMetadataNameOrThrows(input).match( | ||
VALID_STRING_PATTERN, | ||
)?.length, | ||
).toBe(1); | ||
expect(formatMetadataLabelToMetadataNameOrThrows(input)).toEqual(input); | ||
}); | ||
|
||
it('format strings with non latin characters', () => { | ||
const input = 'בְרִבְרִ'; | ||
const expected = 'bRibRi'; | ||
|
||
expect( | ||
formatMetadataLabelToMetadataNameOrThrows(input).match( | ||
VALID_STRING_PATTERN, | ||
)?.length, | ||
).toBe(1); | ||
expect(formatMetadataLabelToMetadataNameOrThrows(input)).toEqual(expected); | ||
}); | ||
|
||
it('format strings with mixed characters', () => { | ||
const input = 'aa2בְרִבְרִ'; | ||
const expected = 'aa2BRibRi'; | ||
|
||
expect( | ||
formatMetadataLabelToMetadataNameOrThrows(input).match( | ||
VALID_STRING_PATTERN, | ||
)?.length, | ||
).toBe(1); | ||
expect(formatMetadataLabelToMetadataNameOrThrows(input)).toEqual(expected); | ||
}); | ||
|
||
it('throws error if could not format', () => { | ||
const input = '$$$***'; | ||
|
||
expect(() => formatMetadataLabelToMetadataNameOrThrows(input)).toThrow(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
...es/twenty-front/src/pages/settings/data-model/utils/format-metadata-label-to-name.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import toCamelCase from 'lodash.camelcase'; | ||
import { slugify, transliterate } from 'transliteration'; | ||
|
||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
const VALID_STRING_PATTERN = /^[a-zA-Z][a-zA-Z0-9 ]*$/; | ||
|
||
export const formatMetadataLabelToMetadataNameOrThrows = ( | ||
string: string, | ||
): string => { | ||
let formattedString = string; | ||
|
||
if (isDefined(formattedString.match(VALID_STRING_PATTERN))) { | ||
return toCamelCase(formattedString); | ||
} | ||
|
||
formattedString = toCamelCase( | ||
slugify(transliterate(formattedString, { trim: true })), | ||
); | ||
|
||
if (!formattedString.match(VALID_STRING_PATTERN)) { | ||
throw new Error(`"${string}" is not a valid name`); | ||
} | ||
|
||
return formattedString; | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/twenty-server/src/engine/metadata-modules/errors/InvalidStringException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
export class InvalidStringException extends BadRequestException { | ||
constructor(string: string) { | ||
const message = `String "${string}" is not valid`; | ||
|
||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
import { InvalidStringException } from 'src/engine/metadata-modules/errors/InvalidStringException'; | ||
import { CreateObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/create-object.input'; | ||
import { UpdateObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input'; | ||
import { validateMetadataName } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; | ||
|
||
export const validateObjectMetadataInput = < | ||
T extends UpdateObjectInput | CreateObjectInput, | ||
>( | ||
objectMetadataInput: T, | ||
): void => { | ||
try { | ||
if (objectMetadataInput.nameSingular) { | ||
validateMetadataName(objectMetadataInput.nameSingular); | ||
} | ||
|
||
if (objectMetadataInput.namePlural) { | ||
validateMetadataName(objectMetadataInput.namePlural); | ||
} | ||
} catch (error) { | ||
if (error instanceof InvalidStringException) { | ||
console.error(error.message); | ||
throw new BadRequestException( | ||
`Characters used in name "${objectMetadataInput.nameSingular}" or "${objectMetadataInput.namePlural}" are not supported`, | ||
); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../twenty-server/src/engine/metadata-modules/utils/__tests__/validate-metadata-name.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { InvalidStringException } from 'src/engine/metadata-modules/errors/InvalidStringException'; | ||
import { validateMetadataName } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils'; | ||
|
||
describe('validateMetadataName', () => { | ||
it('does not throw if string is valid', () => { | ||
const input = 'testName'; | ||
|
||
expect(validateMetadataName(input)).not.toThrow; | ||
}); | ||
|
||
it('throws error if string has non latin characters', () => { | ||
const input = 'בְרִבְרִ'; | ||
|
||
expect(() => validateMetadataName(input)).toThrow(InvalidStringException); | ||
}); | ||
|
||
it('throws error if starts with digits', () => { | ||
const input = '123string'; | ||
|
||
expect(() => validateMetadataName(input)).toThrow(InvalidStringException); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/twenty-server/src/engine/metadata-modules/utils/validate-metadata-name.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { InvalidStringException } from 'src/engine/metadata-modules/errors/InvalidStringException'; | ||
|
||
const VALID_STRING_PATTERN = /^[a-zA-Z][a-zA-Z0-9 ]*$/; | ||
|
||
export const validateMetadataName = (string: string) => { | ||
if (!string.match(VALID_STRING_PATTERN)) { | ||
throw new InvalidStringException(string); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters