Fix transliteration for metadata + transliterate select options (#5430)
## Context Fixes #5403 Transliteration is now integrated to form validation through the schema. While it does not impede inputting an invalid value, it impedes submitting a form that will fail as the transliteration is not possible. Until then we were only performing the transliteration at save time in the front-end, but it's best to provide the information as soon as possible. Later we will add helpers to guide the user (eg "This name is not valid": https://github.com/twentyhq/twenty/issues/5428). --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
import { computeMetadataNameFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-metadata-name-from-label.utils';
|
||||
|
||||
describe('computeMetadataNameFromLabel', () => {
|
||||
it('throws if empty label', () => {
|
||||
const label = ' ';
|
||||
|
||||
expect(() => computeMetadataNameFromLabelOrThrow(label)).toThrow();
|
||||
});
|
||||
|
||||
it('computes name for 1 char long label', () => {
|
||||
const label = 'a';
|
||||
|
||||
expect(computeMetadataNameFromLabelOrThrow(label)).toEqual('a');
|
||||
});
|
||||
|
||||
it('throws if label starts with digits', () => {
|
||||
const label = '1string';
|
||||
|
||||
expect(() => computeMetadataNameFromLabelOrThrow(label)).toThrow();
|
||||
});
|
||||
|
||||
it('computes name for label with non-latin char', () => {
|
||||
const label = 'λλλ!';
|
||||
|
||||
expect(computeMetadataNameFromLabelOrThrow(label)).toEqual('lll');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,27 @@
|
||||
import { computeOptionValueFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-option-value-from-label.utils';
|
||||
|
||||
describe('computeOptionValueFromLabel', () => {
|
||||
it('throws if empty label', () => {
|
||||
const label = ' ';
|
||||
|
||||
expect(() => computeOptionValueFromLabelOrThrow(label)).toThrow();
|
||||
});
|
||||
|
||||
it('computes name for 1 char long label', () => {
|
||||
const label = 'a';
|
||||
|
||||
expect(computeOptionValueFromLabelOrThrow(label)).toEqual('a');
|
||||
});
|
||||
|
||||
it('compute name if starts with digits', () => {
|
||||
const label = '1';
|
||||
|
||||
expect(computeOptionValueFromLabelOrThrow(label)).toEqual('1');
|
||||
});
|
||||
|
||||
it('computes name for label with non-latin char', () => {
|
||||
const label = 'λλλ';
|
||||
|
||||
expect(computeOptionValueFromLabelOrThrow(label)).toEqual('lll');
|
||||
});
|
||||
});
|
||||
@ -1,57 +0,0 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { METADATA_NAME_VALID_PATTERN } from '~/pages/settings/data-model/constants/MetadataNameValidPattern';
|
||||
import { transliterateAndFormatOrThrow } from '~/pages/settings/data-model/utils/transliterate-and-format.utils';
|
||||
|
||||
export const computeMetadataNameFromLabelOrThrow = (label: string): string => {
|
||||
return transliterateAndFormatOrThrow(label, METADATA_NAME_VALID_PATTERN);
|
||||
};
|
||||
@ -0,0 +1,6 @@
|
||||
import { OPTION_VALUE_VALID_PATTERN } from '~/pages/settings/data-model/constants/OptionValueValidPattern';
|
||||
import { transliterateAndFormatOrThrow } from '~/pages/settings/data-model/utils/transliterate-and-format.utils';
|
||||
|
||||
export const computeOptionValueFromLabelOrThrow = (label: string): string => {
|
||||
return transliterateAndFormatOrThrow(label, OPTION_VALUE_VALID_PATTERN);
|
||||
};
|
||||
@ -3,14 +3,13 @@ import { slugify, transliterate } from 'transliteration';
|
||||
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
const VALID_STRING_PATTERN = /^[a-zA-Z][a-zA-Z0-9 ]*$/;
|
||||
|
||||
export const formatMetadataLabelToMetadataNameOrThrows = (
|
||||
export const transliterateAndFormatOrThrow = (
|
||||
string: string,
|
||||
validStringPattern: RegExp,
|
||||
): string => {
|
||||
let formattedString = string;
|
||||
|
||||
if (isDefined(formattedString.match(VALID_STRING_PATTERN))) {
|
||||
if (isDefined(formattedString.match(validStringPattern))) {
|
||||
return toCamelCase(formattedString);
|
||||
}
|
||||
|
||||
@ -18,7 +17,7 @@ export const formatMetadataLabelToMetadataNameOrThrows = (
|
||||
slugify(transliterate(formattedString, { trim: true })),
|
||||
);
|
||||
|
||||
if (!formattedString.match(VALID_STRING_PATTERN)) {
|
||||
if (!formattedString.match(validStringPattern)) {
|
||||
throw new Error(`"${string}" is not a valid name`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user