-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fix] Support non latin characters in schema names #5063
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
753a077
Introduce transliteration lib and util
ijreilly 333baf6
Format object, fields, relation names before create and save
ijreilly 4d81576
Remove char value restriction on field and relation
ijreilly cd075cb
Fix issues with object and fields updates
ijreilly 7772bbb
Fix format-string test
ijreilly f2d5b8f
Stop handling digit prefixes
ijreilly e3c8a5d
Merge branch 'main' of github.com:twentyhq/twenty into fix--support-n…
ijreilly bce9e22
Perform transliteration in the front-end
ijreilly 6e51aa8
validate object name in the back-end
ijreilly b7ebc2b
Fix test file for format-string
ijreilly 70a3431
Improve naming and test writing
ijreilly f22f637
Throw error in formatting does not work
ijreilly 961b924
fix renamed util
Weiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 0 additions & 8 deletions
8
...es/twenty-front/src/modules/object-metadata/utils/__tests__/validateMetadataLabel.test.ts
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
packages/twenty-front/src/modules/object-metadata/utils/validateMetadataLabel.ts
This file was deleted.
Oops, something went wrong.
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
9 changes: 9 additions & 0 deletions
9
packages/twenty-server/src/engine/metadata-modules/errors/CharactersNotSupportedException.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 ChararactersNotSupportedException extends BadRequestException { | ||
constructor(string: string) { | ||
const message = `String "${string}" contains unsupported characters`; | ||
|
||
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
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
33 changes: 33 additions & 0 deletions
33
...er/src/engine/metadata-modules/object-metadata/utils/format-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,33 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
import { ChararactersNotSupportedException } from 'src/engine/metadata-modules/errors/CharactersNotSupportedException'; | ||
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 { formatString } from 'src/engine/metadata-modules/utils/format-string.util'; | ||
|
||
export const formatObjectMetadataInput = < | ||
T extends UpdateObjectInput | CreateObjectInput, | ||
>( | ||
objectMetadataInput: T, | ||
): T => { | ||
try { | ||
return (objectMetadataInput = { | ||
...objectMetadataInput, | ||
nameSingular: objectMetadataInput.nameSingular | ||
? formatString(objectMetadataInput.nameSingular) | ||
: objectMetadataInput.nameSingular, | ||
namePlural: objectMetadataInput.namePlural | ||
? formatString(objectMetadataInput.namePlural) | ||
: objectMetadataInput.namePlural, | ||
}); | ||
} catch (error) { | ||
if (error instanceof ChararactersNotSupportedException) { | ||
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
36 changes: 36 additions & 0 deletions
36
packages/twenty-server/src/engine/metadata-modules/utils/__tests__/format-string.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,36 @@ | ||
import { | ||
formatString, | ||
validPattern, | ||
} from 'src/engine/metadata-modules/utils/format-string.util'; | ||
|
||
describe('formatString', () => { | ||
it('format strings starting with digits', () => { | ||
const input = '123string'; | ||
const expected = 'string123'; | ||
|
||
expect(formatString(input).match(validPattern)?.length).toBe(1); | ||
expect(formatString(input)).toEqual(expected); | ||
|
||
const inputCapitalized = '123String'; | ||
const expectedCamelCased = 'string123'; | ||
|
||
expect(formatString(inputCapitalized).match(validPattern)?.length).toBe(1); | ||
expect(formatString(inputCapitalized)).toEqual(expectedCamelCased); | ||
}); | ||
|
||
it('format strings with non latin characters', () => { | ||
const input = 'בְרִבְרִ'; | ||
const expected = 'bRibRiTransliterated'; | ||
|
||
expect(formatString(input).match(validPattern)?.length).toBe(1); | ||
expect(formatString(input)).toEqual(expected); | ||
}); | ||
|
||
it('format strings with mixed characters', () => { | ||
const input = '2aaבְרִבְרִ'; | ||
const expected = 'aabRibRi2Transliterated'; | ||
|
||
expect(formatString(input).match(validPattern)?.length).toBe(1); | ||
expect(formatString(input)).toEqual(expected); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/twenty-server/src/engine/metadata-modules/utils/format-string.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,36 @@ | ||
import { transliterate, slugify } from 'transliteration'; | ||
import toCamelCase from 'lodash.camelcase'; | ||
|
||
import { ChararactersNotSupportedException } from 'src/engine/metadata-modules/errors/CharactersNotSupportedException'; | ||
|
||
export const validPattern = /^[a-zA-Z][a-zA-Z0-9 ]*$/; | ||
const startsWithDigitPattern = /^[^\d]*(\d+)/; | ||
|
||
export const formatString = (string: string): string => { | ||
let formattedString = string; | ||
|
||
if (formattedString.match(validPattern)) { | ||
return formattedString; | ||
} | ||
|
||
if (formattedString.match(startsWithDigitPattern)) { | ||
const digitsAtStart = formattedString.match(startsWithDigitPattern)?.[0]; | ||
|
||
formattedString = | ||
formattedString.slice(digitsAtStart?.length || 0) + digitsAtStart; | ||
} | ||
|
||
if (formattedString.match(validPattern)) { | ||
return toCamelCase(formattedString); | ||
} | ||
|
||
formattedString = toCamelCase( | ||
slugify(transliterate(formattedString, { trim: true }) + '-transliterated'), | ||
); | ||
|
||
if (!formattedString.match(validPattern)) { | ||
throw new ChararactersNotSupportedException(string); | ||
} | ||
|
||
return formattedString; | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.npmjs.com/package/transliteration