Fix: multi-select default values validation (#12271)
https://github.com/user-attachments/assets/3bea63cc-b098-4252-8787-fc6263f01e8d Closes #12277 --------- Co-authored-by: prastoin <paul@twenty.com> Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,68 @@
|
||||
import { EachTestingContext } from 'twenty-shared/testing';
|
||||
|
||||
import { trimAndRemoveDuplicatedWhitespacesFromString } from 'src/utils/trim-and-remove-duplicated-whitespaces-from-string';
|
||||
|
||||
type TrimAndRemoveWhitespacesTestCase = EachTestingContext<{
|
||||
input: string;
|
||||
expected: string;
|
||||
}>;
|
||||
|
||||
describe('trim-and-remove-duplicated-whitespaces-from-string', () => {
|
||||
const testCases: TrimAndRemoveWhitespacesTestCase[] = [
|
||||
{
|
||||
title: 'should trim and remove duplicated whitespaces from a string',
|
||||
context: {
|
||||
input: ' Hello World ',
|
||||
expected: 'Hello World',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle string with multiple spaces between words',
|
||||
context: {
|
||||
input: 'This is a test',
|
||||
expected: 'This is a test',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle string with only whitespaces',
|
||||
context: {
|
||||
input: ' ',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle empty string',
|
||||
context: {
|
||||
input: '',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle string with tabs and newlines',
|
||||
context: {
|
||||
input: 'Hello\t\t\tWorld\n\n Test',
|
||||
expected: 'Hello World Test',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle string with leading and trailing spaces',
|
||||
context: {
|
||||
input: ' Leading and trailing spaces ',
|
||||
expected: 'Leading and trailing spaces',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should preserve single spaces between words',
|
||||
context: {
|
||||
input: 'This is already properly spaced',
|
||||
expected: 'This is already properly spaced',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
test.each(testCases)('$title', ({ context: { input, expected } }) => {
|
||||
const result = trimAndRemoveDuplicatedWhitespacesFromString(input);
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@ -1,4 +1,4 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { trimAndRemoveDuplicatedWhitespacesFromString } from 'src/utils/trim-and-remove-duplicated-whitespaces-from-string';
|
||||
|
||||
type OnlyStringPropertiesKey<T> = Extract<keyof T, string>;
|
||||
|
||||
@ -8,9 +8,6 @@ type StringPropertyKeys<T> = {
|
||||
: never;
|
||||
}[OnlyStringPropertiesKey<T>];
|
||||
|
||||
const sanitizeString = (str: string | null) =>
|
||||
isDefined(str) ? str.trim().replace(/\s+/g, ' ') : str;
|
||||
|
||||
export const trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties = <T>(
|
||||
obj: T,
|
||||
keys: StringPropertyKeys<T>[],
|
||||
@ -18,13 +15,17 @@ export const trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties = <T>(
|
||||
return keys.reduce((acc, key) => {
|
||||
const occurrence = acc[key];
|
||||
|
||||
if (occurrence === undefined || typeof occurrence !== 'string') {
|
||||
if (
|
||||
occurrence === undefined ||
|
||||
typeof occurrence !== 'string' ||
|
||||
occurrence === null
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: sanitizeString(acc[key] as string | null),
|
||||
[key]: trimAndRemoveDuplicatedWhitespacesFromString(occurrence),
|
||||
};
|
||||
}, obj);
|
||||
};
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
const MULTIPLE_WHITESPACE_REGEX = /\s+/g;
|
||||
|
||||
export const trimAndRemoveDuplicatedWhitespacesFromString = (str: string) =>
|
||||
str.trim().replace(MULTIPLE_WHITESPACE_REGEX, ' ');
|
||||
Reference in New Issue
Block a user