validation on Select field (#8316)

fix #8204
I changed "API keys" to "API values".
Stopped inputting special characters in Select field option keys.

@lucasbordeau please check the changes and tell me if I need to do any
other changes. :)

---------

Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
Ketan Mehta
2024-11-14 22:16:18 +05:30
committed by GitHub
parent 15b8b9b158
commit 51c54d4c5b
22 changed files with 96 additions and 204 deletions

View File

@ -1,5 +1,4 @@
import toCamelCase from 'lodash.camelcase';
import { slugify, transliterate } from 'transliteration';
import { slugify } from 'transliteration';
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';
@ -63,30 +62,6 @@ export const validateObjectMetadataInputOrThrow = <
validateNameIsNotTooLongThrow(objectMetadataInput.namePlural);
};
export const transliterateAndFormatOrThrow = (string?: string): string => {
if (!string) {
throw new ObjectMetadataException(
'Name is required',
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
let formattedString = string;
if (formattedString.match(METADATA_NAME_VALID_PATTERN) !== null) {
return toCamelCase(formattedString);
}
formattedString = toCamelCase(
slugify(transliterate(formattedString, { trim: true })),
);
if (!formattedString.match(METADATA_NAME_VALID_PATTERN)) {
throw new Error(`"${string}" is not a valid name`);
}
return formattedString;
};
const validateNameIsNotReservedKeywordOrThrow = (name?: string) => {
if (name) {
if (reservedKeywords.includes(name)) {
@ -133,17 +108,41 @@ const validateNameCharactersOrThrow = (name?: string) => {
}
};
export const computeMetadataNameFromLabelOrThrow = (label: string): string => {
const formattedString = transliterateAndFormatOrThrow(label);
export const computeMetadataNameFromLabel = (label: string): string => {
if (!label) {
throw new ObjectMetadataException(
'Label is required',
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
return formattedString;
const prefixedLabel = /^\d/.test(label) ? `n${label}` : label;
if (prefixedLabel === '') {
return '';
}
const formattedString = slugify(prefixedLabel, {
trim: true,
separator: '_',
allowedChars: 'a-zA-Z0-9',
});
if (formattedString === '') {
throw new ObjectMetadataException(
`Invalid label: "${label}"`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
return camelCase(formattedString);
};
export const validateNameAndLabelAreSyncOrThrow = (
label: string,
name: string,
) => {
const computedName = computeMetadataNameFromLabelOrThrow(label);
const computedName = computeMetadataNameFromLabel(label);
if (name !== computedName) {
throw new ObjectMetadataException(