29a4f4d685
# 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
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { trimAndRemoveDuplicatedWhitespacesFromString } from '@/utils/trim-and-remove-duplicated-whitespaces-from-string';
|
|
|
|
type OnlyStringPropertiesKey<T> = Extract<keyof T, string>;
|
|
|
|
export type StringPropertyKeys<T> = {
|
|
[K in OnlyStringPropertiesKey<T>]: T[K] extends string | undefined
|
|
? K
|
|
: never;
|
|
}[OnlyStringPropertiesKey<T>];
|
|
|
|
export const trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties = <
|
|
T,
|
|
TKeys extends StringPropertyKeys<T>[],
|
|
TExtract extends boolean = false,
|
|
>(
|
|
obj: T,
|
|
keys: TKeys,
|
|
extractKeys?: TExtract,
|
|
): TExtract extends true
|
|
? {
|
|
[P in TKeys[number]]: T[P];
|
|
}
|
|
: T => {
|
|
return keys.reduce(
|
|
(acc, key) => {
|
|
const occurrence = obj[key];
|
|
|
|
if (
|
|
occurrence === undefined ||
|
|
typeof occurrence !== 'string' ||
|
|
occurrence === null
|
|
) {
|
|
return acc;
|
|
}
|
|
|
|
return {
|
|
...acc,
|
|
[key]: trimAndRemoveDuplicatedWhitespacesFromString(occurrence),
|
|
};
|
|
},
|
|
extractKeys ? ({} as T) : obj,
|
|
);
|
|
};
|