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:
@ -1 +1 @@
|
||||
export const METADATA_LABEL_VALID_PATTERN = /^[^0-9].*$/;
|
||||
export const METADATA_LABEL_VALID_PATTERN = /^.*$/;
|
||||
|
||||
@ -1 +1 @@
|
||||
export const OPTION_VALUE_VALID_PATTERN = /^[a-zA-Z0-9]+$/;
|
||||
export const OPTION_VALUE_VALID_PATTERN = /^[A-Z_][A-Z0-9_]*$/;
|
||||
|
||||
@ -1,27 +1,9 @@
|
||||
import { computeMetadataNameFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-metadata-name-from-label.utils';
|
||||
import { computeMetadataNameFromLabel } 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');
|
||||
expect(computeMetadataNameFromLabel(label)).toEqual('lll');
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
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,9 +1,22 @@
|
||||
import { METADATA_NAME_VALID_PATTERN } from '~/pages/settings/data-model/constants/MetadataNameValidPattern';
|
||||
import { transliterateAndFormatOrThrow } from '~/pages/settings/data-model/utils/transliterate-and-format.utils';
|
||||
import camelCase from 'lodash.camelcase';
|
||||
import { slugify } from 'transliteration';
|
||||
|
||||
export const computeMetadataNameFromLabelOrThrow = (label: string): string => {
|
||||
if (label === '') {
|
||||
export const computeMetadataNameFromLabel = (label: string): string => {
|
||||
const prefixedLabel = /^\d/.test(label) ? `n${label}` : label;
|
||||
|
||||
if (prefixedLabel === '') {
|
||||
return '';
|
||||
}
|
||||
return transliterateAndFormatOrThrow(label, METADATA_NAME_VALID_PATTERN);
|
||||
|
||||
const formattedString = slugify(prefixedLabel, {
|
||||
trim: true,
|
||||
separator: '_',
|
||||
allowedChars: 'a-zA-Z0-9',
|
||||
});
|
||||
|
||||
if (formattedString === '') {
|
||||
throw new Error('Invalid label');
|
||||
}
|
||||
|
||||
return camelCase(formattedString);
|
||||
};
|
||||
|
||||
@ -1,6 +1,17 @@
|
||||
import { OPTION_VALUE_VALID_PATTERN } from '~/pages/settings/data-model/constants/OptionValueValidPattern';
|
||||
import { transliterateAndFormatOrThrow } from '~/pages/settings/data-model/utils/transliterate-and-format.utils';
|
||||
import { slugify } from 'transliteration';
|
||||
|
||||
export const computeOptionValueFromLabelOrThrow = (label: string): string => {
|
||||
return transliterateAndFormatOrThrow(label, OPTION_VALUE_VALID_PATTERN);
|
||||
export const computeOptionValueFromLabel = (label: string): string => {
|
||||
const prefixedLabel = /^\d/.test(label) ? `OPT${label}` : label;
|
||||
|
||||
const formattedString = slugify(prefixedLabel, {
|
||||
trim: true,
|
||||
separator: '_',
|
||||
allowedChars: 'a-zA-Z0-9_',
|
||||
});
|
||||
|
||||
if (formattedString === '') {
|
||||
throw new Error('Invalid label');
|
||||
}
|
||||
|
||||
return formattedString.toUpperCase();
|
||||
};
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user