## 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>
26 lines
644 B
TypeScript
26 lines
644 B
TypeScript
import toCamelCase from 'lodash.camelcase';
|
|
import { slugify, transliterate } from 'transliteration';
|
|
|
|
import { isDefined } from '~/utils/isDefined';
|
|
|
|
export const transliterateAndFormatOrThrow = (
|
|
string: string,
|
|
validStringPattern: RegExp,
|
|
): string => {
|
|
let formattedString = string;
|
|
|
|
if (isDefined(formattedString.match(validStringPattern))) {
|
|
return toCamelCase(formattedString);
|
|
}
|
|
|
|
formattedString = toCamelCase(
|
|
slugify(transliterate(formattedString, { trim: true })),
|
|
);
|
|
|
|
if (!formattedString.match(validStringPattern)) {
|
|
throw new Error(`"${string}" is not a valid name`);
|
|
}
|
|
|
|
return formattedString;
|
|
};
|