The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { msg } from '@lingui/core/macro';
import {
IDENTIFIER_MAX_CHAR_LENGTH,
RESERVED_METADATA_NAME_KEYWORDS,
} from 'twenty-shared/metadata';
import { type FlatObjectMetadataValidationError } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata-validation-error.type';
import { type ObjectMetadataMinimalInformation } from 'src/engine/metadata-modules/flat-object-metadata/types/object-metadata-minimal-information.type';
import { ObjectMetadataExceptionCode } from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { IDENTIFIER_MIN_CHAR_LENGTH } from 'src/engine/metadata-modules/utils/constants/identifier-min-char-length.constants';
import { type UniversalFlatObjectMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-object-metadata.type';
import { camelCase } from 'src/utils/camel-case';
const STARTS_WITH_LOWER_CASE_AND_CONTAINS_ONLY_CAPS_AND_LOWER_LETTERS_AND_NUMBER_STRING_REGEX =
/^[a-z][a-zA-Z0-9]*$/;
export const validateFlatObjectMetadataNames = ({
namePlural,
nameSingular,
}: Pick<UniversalFlatObjectMetadata, 'nameSingular' | 'namePlural'> &
ObjectMetadataMinimalInformation) => {
const errors: FlatObjectMetadataValidationError[] = [];
for (const name of [nameSingular, namePlural]) {
if (name.length > IDENTIFIER_MAX_CHAR_LENGTH) {
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `Name is too long`,
userFriendlyMessage: msg`Name is too long`,
value: name,
});
}
if (name.length < IDENTIFIER_MIN_CHAR_LENGTH) {
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `Name is too short`,
userFriendlyMessage: msg`Name is too short`,
value: name,
});
}
if (
!name.match(
STARTS_WITH_LOWER_CASE_AND_CONTAINS_ONLY_CAPS_AND_LOWER_LETTERS_AND_NUMBER_STRING_REGEX,
)
) {
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `Name is not valid: it must start with lowercase letter and contain only alphanumeric letters`,
userFriendlyMessage: msg`Name is not valid: it must start with lowercase letter and contain only alphanumeric letters`,
value: name,
});
} else if (camelCase(name) !== name) {
// GraphQL resolver and type names are derived through camelCase
const camelCasedName = camelCase(name);
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `Name is not valid: it must be camelCase, e.g. "${camelCasedName}"`,
userFriendlyMessage: msg`Name is not valid: it must be camelCase, e.g. "${camelCasedName}"`,
value: name,
});
}
if (RESERVED_METADATA_NAME_KEYWORDS.includes(name)) {
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `This name is reserved. Use a different name or the system will add "Custom" suffix.`,
userFriendlyMessage: msg`This name is reserved. Use a different name or the system will add "Custom" suffix.`,
value: name,
});
}
}
const namesAreIdentical =
namePlural.trim().toLowerCase() === nameSingular.trim().toLowerCase();
if (namesAreIdentical) {
errors.push({
code: ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
message: `The singular and plural names cannot be the same for an object`,
userFriendlyMessage: msg`The singular and plural names cannot be the same for an object`,
value: namePlural,
});
}
return errors;
};