Files
twenty/packages/twenty-shared/src/utils/__tests__/sanitizeObjectStringFields.test.ts
T
Paul RastoinandGitHub 29a4f4d685 CreateFieldInput transpilation to FlatFieldMetadata, FlatFieldMetadata validation (#13493)
# Introduction
Following https://github.com/twentyhq/twenty/pull/13420

What has been done:
- `CreateFieldInput` transpilation to `FlatFieldMetadata`
- `FlatFieldMetadata` validator service
- A lof of transpilation utils from `input` to `flatObject` or
`flatField`
- Created dedicated v2 api metadata services
- Introducing `inferDeletionFromMissingObjectFieldIndex` in the builder,
to avoid diffing every object and field of the current workspace we
allow only generating create/update migration operations, usefull when
passing by the api metadata


## We still need to in another PR:
- Implement a strong unit test coverage and critical functions and
services
- Finalize flat field metadata validation exception for `options`
`defaultValue` `settings` and `relations`
- Finalize `flatObjectMetadata` validation and v2 service refactor
- Plug the new service when feature flag is enabled
2025-07-30 15:08:11 +02:00

181 lines
4.2 KiB
TypeScript

import { EachTestingContext } from '@/testing/types/EachTestingContext.type';
import { sanitizeObjectStringFields } from '../sanitizeObjectStringFields';
type TestObject = {
name?: string;
age?: number | null;
city?: string | undefined;
description?: string;
user?: {
name: string;
contact: {
email: string;
};
};
tags?: string[];
items?: Array<{ name: string }>;
mixedArray?: Array<string | number | null | { text: string }>;
};
type SanitizeTestCase = EachTestingContext<{
input: {
obj: TestObject;
keys: (keyof TestObject)[];
};
expected: object;
}>;
describe('sanitizeObjectStringFields', () => {
const testCases: SanitizeTestCase[] = [
{
title: 'should handle basic string properties and trim whitespaces',
context: {
input: {
obj: { name: ' John Doe ', age: 30 },
keys: ['name', 'age'],
},
expected: { name: 'John Doe', age: 30 },
},
},
{
title: 'should handle nested objects',
context: {
input: {
obj: {
user: {
name: ' Jane Smith ',
contact: { email: ' jane@example.com ' },
},
},
keys: ['user'],
},
expected: {
user: {
name: 'Jane Smith',
contact: { email: 'jane@example.com' },
},
},
},
},
{
title: 'should skip undefined and null values',
context: {
input: {
obj: { name: ' John ', age: null, city: undefined },
keys: ['name', 'age', 'city'],
},
expected: { name: 'John', age: null, city: undefined },
},
},
{
title: 'should handle empty object',
context: {
input: {
obj: {},
keys: ['name', 'age'],
},
expected: {},
},
},
{
title: 'should handle object with no matching keys',
context: {
input: {
obj: { name: 'John', age: 30 },
keys: ['city', 'name'],
},
expected: { name: 'John', age: 30 },
},
},
{
title: 'should handle string with multiple spaces, tabs and newlines',
context: {
input: {
obj: { description: 'This is\t\ta\n\ntest string' },
keys: ['description'],
},
expected: { description: 'This is a test string' },
},
},
{
title: 'should handle array of strings within object',
context: {
input: {
obj: { tags: [' tag1 ', ' tag2 ', 'test string '] },
keys: ['tags'],
},
expected: { tags: ['tag1', 'tag2', 'test string'] },
},
},
{
title: 'should handle array of objects within object',
context: {
input: {
obj: {
items: [{ name: ' John Doe ' }, { name: ' Jane Smith ' }],
},
keys: ['items'],
},
expected: {
items: [{ name: 'John Doe' }, { name: 'Jane Smith' }],
},
},
},
{
title: 'should handle nested arrays within object properties',
context: {
input: {
obj: {
tags: [' tag1 ', ' tag2 '],
user: {
name: ' John Doe ',
contact: { email: ' john@example.com ' },
},
},
keys: ['tags', 'user'],
},
expected: {
tags: ['tag1', 'tag2'],
user: {
name: 'John Doe',
contact: { email: 'john@example.com' },
},
},
},
},
{
title: 'should handle mixed content array within object',
context: {
input: {
obj: {
mixedArray: [
' string ',
123,
null,
{ text: ' nested text ' },
],
},
keys: ['mixedArray'],
},
expected: {
mixedArray: ['string', 123, null, { text: 'nested text' }],
},
},
},
];
test.each(testCases)(
'$title',
({
context: {
input: { obj, keys },
expected,
},
}) => {
const result = sanitizeObjectStringFields(obj, keys);
expect(result).toEqual(expected);
},
);
});