Twenty config integration tests + conversion refactor (#11972)
- In this PR the default value of IS_CONFIG_VARIABLES_IN_DB_ENABLED has
been changed to true,
- This is my first time writing integration tests, so I’d appreciate a
thorough review. :)
I’ve tried to follow the existing test patterns closely, but there might
be some small mistakes I may have missed.
Also let me know if I have missed any important test cases that should
be tested
UPDATE -
### Config Value Converter Refactoring
- Created a centralized type transformers registry with bidirectional
validation
- Refactored ConfigValueConverterService to support validation in both
directions:
- Maintained existing DB-to-app conversion behavior
- Added validation for app-to-DB conversion
- Added integration tests to verify validation works in both directions
---------
Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
@ -762,7 +762,7 @@ export class ConfigVariables {
|
||||
type: ConfigVariableType.BOOLEAN,
|
||||
})
|
||||
@IsOptional()
|
||||
IS_CONFIG_VARIABLES_IN_DB_ENABLED = false;
|
||||
IS_CONFIG_VARIABLES_IN_DB_ENABLED = true;
|
||||
|
||||
@ConfigVariablesMetadata({
|
||||
group: ConfigVariablesGroup.TokensDuration,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -4,8 +4,11 @@ import { ConfigVariables } from 'src/engine/core-modules/twenty-config/config-va
|
||||
import { CONFIG_VARIABLES_INSTANCE_TOKEN } from 'src/engine/core-modules/twenty-config/constants/config-variables-instance-tokens.constants';
|
||||
import { ConfigVariablesMetadataMap } from 'src/engine/core-modules/twenty-config/decorators/config-variables-metadata.decorator';
|
||||
import { ConfigVariableType } from 'src/engine/core-modules/twenty-config/enums/config-variable-type.enum';
|
||||
import { ConfigVariableOptions } from 'src/engine/core-modules/twenty-config/types/config-variable-options.type';
|
||||
import { configTransformers } from 'src/engine/core-modules/twenty-config/utils/config-transformers.util';
|
||||
import {
|
||||
ConfigVariableException,
|
||||
ConfigVariableExceptionCode,
|
||||
} from 'src/engine/core-modules/twenty-config/twenty-config.exception';
|
||||
import { typeTransformers } from 'src/engine/core-modules/twenty-config/utils/type-transformers.registry';
|
||||
import { TypedReflect } from 'src/utils/typed-reflect';
|
||||
|
||||
@Injectable()
|
||||
@ -30,164 +33,62 @@ export class ConfigValueConverterService {
|
||||
const options = metadata?.options;
|
||||
|
||||
try {
|
||||
switch (configType) {
|
||||
case ConfigVariableType.BOOLEAN: {
|
||||
const result = configTransformers.boolean(dbValue);
|
||||
const transformer = typeTransformers[configType];
|
||||
|
||||
if (result !== undefined && typeof result !== 'boolean') {
|
||||
throw new Error(
|
||||
`Expected boolean for key ${key}, got ${typeof result}`,
|
||||
);
|
||||
}
|
||||
|
||||
return result as ConfigVariables[T];
|
||||
}
|
||||
|
||||
case ConfigVariableType.NUMBER: {
|
||||
const result = configTransformers.number(dbValue);
|
||||
|
||||
if (result !== undefined && typeof result !== 'number') {
|
||||
throw new Error(
|
||||
`Expected number for key ${key}, got ${typeof result}`,
|
||||
);
|
||||
}
|
||||
|
||||
return result as ConfigVariables[T];
|
||||
}
|
||||
|
||||
case ConfigVariableType.STRING: {
|
||||
const result = configTransformers.string(dbValue);
|
||||
|
||||
if (result !== undefined && typeof result !== 'string') {
|
||||
throw new Error(
|
||||
`Expected string for key ${key}, got ${typeof result}`,
|
||||
);
|
||||
}
|
||||
|
||||
return result as ConfigVariables[T];
|
||||
}
|
||||
|
||||
case ConfigVariableType.ARRAY: {
|
||||
const result = this.convertToArray(dbValue, options);
|
||||
|
||||
if (result !== undefined && !Array.isArray(result)) {
|
||||
throw new Error(
|
||||
`Expected array for key ${key}, got ${typeof result}`,
|
||||
);
|
||||
}
|
||||
|
||||
return result as ConfigVariables[T];
|
||||
}
|
||||
|
||||
case ConfigVariableType.ENUM: {
|
||||
const result = this.convertToEnum(dbValue, options);
|
||||
|
||||
return result as ConfigVariables[T];
|
||||
}
|
||||
|
||||
default:
|
||||
return dbValue as ConfigVariables[T];
|
||||
if (!transformer) {
|
||||
return dbValue as ConfigVariables[T];
|
||||
}
|
||||
|
||||
return transformer.toApp(dbValue, options) as ConfigVariables[T];
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
throw new ConfigVariableException(
|
||||
`Failed to convert ${key as string} to app value: ${(error as Error).message}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
convertAppValueToDbValue<T extends keyof ConfigVariables>(
|
||||
appValue: ConfigVariables[T] | null | undefined,
|
||||
key: T,
|
||||
): unknown {
|
||||
if (appValue === undefined || appValue === null) {
|
||||
if (appValue === null || appValue === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof appValue === 'string' ||
|
||||
typeof appValue === 'number' ||
|
||||
typeof appValue === 'boolean'
|
||||
) {
|
||||
return appValue;
|
||||
}
|
||||
const metadata = this.getConfigVariableMetadata(key);
|
||||
const configType = metadata?.type || this.inferTypeFromValue(key);
|
||||
const options = metadata?.options;
|
||||
|
||||
if (Array.isArray(appValue)) {
|
||||
return appValue;
|
||||
}
|
||||
try {
|
||||
const transformer = typeTransformers[configType];
|
||||
|
||||
if (typeof appValue === 'object') {
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(appValue));
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to serialize object value: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Cannot convert value of type ${typeof appValue} to storage format`,
|
||||
);
|
||||
}
|
||||
|
||||
private convertToArray(
|
||||
value: unknown,
|
||||
options?: ConfigVariableOptions,
|
||||
): unknown[] {
|
||||
if (Array.isArray(value)) {
|
||||
return this.validateArrayAgainstOptions(value, options);
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
try {
|
||||
const parsedArray = JSON.parse(value);
|
||||
|
||||
if (Array.isArray(parsedArray)) {
|
||||
return this.validateArrayAgainstOptions(parsedArray, options);
|
||||
if (!transformer) {
|
||||
if (typeof appValue === 'object') {
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(appValue));
|
||||
} catch (error) {
|
||||
throw new ConfigVariableException(
|
||||
`Failed to serialize object value: ${error instanceof Error ? error.message : String(error)}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
const splitArray = value.split(',').map((item) => item.trim());
|
||||
|
||||
return this.validateArrayAgainstOptions(splitArray, options);
|
||||
}
|
||||
}
|
||||
|
||||
return this.validateArrayAgainstOptions([value], options);
|
||||
}
|
||||
|
||||
private validateArrayAgainstOptions(
|
||||
array: unknown[],
|
||||
options?: ConfigVariableOptions,
|
||||
): unknown[] {
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return array;
|
||||
}
|
||||
|
||||
return array.filter((item) => {
|
||||
const included = options.includes(item as string);
|
||||
|
||||
if (!included) {
|
||||
this.logger.debug(
|
||||
`Filtered out array item '${String(item)}' not in allowed options`,
|
||||
);
|
||||
return appValue;
|
||||
}
|
||||
|
||||
return included;
|
||||
});
|
||||
}
|
||||
return transformer.toStorage(appValue as any, options);
|
||||
} catch (error) {
|
||||
if (error instanceof ConfigVariableException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
private convertToEnum(
|
||||
value: unknown,
|
||||
options?: ConfigVariableOptions,
|
||||
): unknown | undefined {
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return value;
|
||||
throw new ConfigVariableException(
|
||||
`Failed to convert ${key as string} to DB value: ${(error as Error).message}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
if (options.includes(value as string)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private getConfigVariableMetadata<T extends keyof ConfigVariables>(key: T) {
|
||||
|
||||
@ -22,6 +22,7 @@ export class ConfigVariableGraphqlApiExceptionFilter
|
||||
case ConfigVariableExceptionCode.ENVIRONMENT_ONLY_VARIABLE:
|
||||
throw new ForbiddenError(exception.message);
|
||||
case ConfigVariableExceptionCode.DATABASE_CONFIG_DISABLED:
|
||||
case ConfigVariableExceptionCode.VALIDATION_FAILED:
|
||||
throw new UserInputError(exception.message);
|
||||
case ConfigVariableExceptionCode.INTERNAL_ERROR:
|
||||
default:
|
||||
|
||||
@ -64,7 +64,7 @@ export class ConfigStorageService implements ConfigStorageInterface {
|
||||
try {
|
||||
const convertedValue = isDecrypt
|
||||
? this.configValueConverter.convertDbValueToAppValue(value, key)
|
||||
: this.configValueConverter.convertAppValueToDbValue(value);
|
||||
: this.configValueConverter.convertAppValueToDbValue(value, key);
|
||||
|
||||
const metadata = this.getConfigMetadata(key);
|
||||
const isSensitiveString =
|
||||
|
||||
@ -16,7 +16,7 @@ import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twent
|
||||
export class TwentyConfigModule extends ConfigurableModuleClass {
|
||||
static forRoot(): DynamicModule {
|
||||
const isConfigVariablesInDbEnabled =
|
||||
process.env.IS_CONFIG_VARIABLES_IN_DB_ENABLED === 'true';
|
||||
process.env.IS_CONFIG_VARIABLES_IN_DB_ENABLED !== 'false';
|
||||
|
||||
const imports = [
|
||||
ConfigModule.forRoot({
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
import { Inject, Injectable, Logger, Optional } from '@nestjs/common';
|
||||
import { Injectable, Logger, Optional } from '@nestjs/common';
|
||||
|
||||
import { isString } from 'class-validator';
|
||||
|
||||
import { ConfigVariables } from 'src/engine/core-modules/twenty-config/config-variables';
|
||||
import { CONFIG_VARIABLES_INSTANCE_TOKEN } from 'src/engine/core-modules/twenty-config/constants/config-variables-instance-tokens.constants';
|
||||
import { CONFIG_VARIABLES_MASKING_CONFIG } from 'src/engine/core-modules/twenty-config/constants/config-variables-masking-config';
|
||||
import { ConfigVariablesMetadataOptions } from 'src/engine/core-modules/twenty-config/decorators/config-variables-metadata.decorator';
|
||||
import { DatabaseConfigDriver } from 'src/engine/core-modules/twenty-config/drivers/database-config.driver';
|
||||
@ -26,8 +25,6 @@ export class TwentyConfigService {
|
||||
constructor(
|
||||
private readonly environmentConfigDriver: EnvironmentConfigDriver,
|
||||
@Optional() private readonly databaseConfigDriver: DatabaseConfigDriver,
|
||||
@Inject(CONFIG_VARIABLES_INSTANCE_TOKEN)
|
||||
private readonly configVariablesInstance: ConfigVariables,
|
||||
) {
|
||||
const isConfigVariablesInDbEnabled = this.environmentConfigDriver.get(
|
||||
'IS_CONFIG_VARIABLES_IN_DB_ENABLED',
|
||||
|
||||
@ -120,7 +120,7 @@ describe('applyBasicValidators', () => {
|
||||
);
|
||||
|
||||
expect(IsString).toHaveBeenCalled();
|
||||
expect(Transform).not.toHaveBeenCalled(); // String doesn't need a transform
|
||||
expect(Transform).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@ -136,7 +136,7 @@ describe('applyBasicValidators', () => {
|
||||
);
|
||||
|
||||
expect(IsEnum).toHaveBeenCalledWith(enumOptions);
|
||||
expect(Transform).not.toHaveBeenCalled(); // Enum doesn't need a transform
|
||||
expect(Transform).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should apply enum validator with enum object options', () => {
|
||||
@ -146,6 +146,25 @@ describe('applyBasicValidators', () => {
|
||||
Option3 = 'value3',
|
||||
}
|
||||
|
||||
jest.mock(
|
||||
'src/engine/core-modules/twenty-config/utils/type-transformers.registry',
|
||||
() => ({
|
||||
typeTransformers: {
|
||||
enum: {
|
||||
getValidators: jest.fn().mockImplementation((options) => {
|
||||
if (options && Object.keys(options).length > 0) {
|
||||
return [IsEnum(options)];
|
||||
}
|
||||
|
||||
return [];
|
||||
}),
|
||||
getTransformers: jest.fn().mockReturnValue([]),
|
||||
},
|
||||
},
|
||||
}),
|
||||
{ virtual: true },
|
||||
);
|
||||
|
||||
applyBasicValidators(
|
||||
ConfigVariableType.ENUM,
|
||||
mockTarget,
|
||||
@ -154,7 +173,7 @@ describe('applyBasicValidators', () => {
|
||||
);
|
||||
|
||||
expect(IsEnum).toHaveBeenCalledWith(TestEnum);
|
||||
expect(Transform).not.toHaveBeenCalled(); // Enum doesn't need a transform
|
||||
expect(Transform).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not apply enum validator without options', () => {
|
||||
@ -178,7 +197,7 @@ describe('applyBasicValidators', () => {
|
||||
);
|
||||
|
||||
expect(IsArray).toHaveBeenCalled();
|
||||
expect(Transform).not.toHaveBeenCalled(); // Array doesn't need a transform
|
||||
expect(Transform).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -0,0 +1,189 @@
|
||||
import { ConfigVariableType } from 'src/engine/core-modules/twenty-config/enums/config-variable-type.enum';
|
||||
import { typeTransformers } from 'src/engine/core-modules/twenty-config/utils/type-transformers.registry';
|
||||
|
||||
describe('Type Transformers Registry', () => {
|
||||
describe('Boolean Transformer', () => {
|
||||
const booleanTransformer = typeTransformers[ConfigVariableType.BOOLEAN];
|
||||
|
||||
describe('toApp', () => {
|
||||
it('should convert string "true" to boolean true', () => {
|
||||
expect(booleanTransformer.toApp('true')).toBe(true);
|
||||
});
|
||||
|
||||
it('should convert string "false" to boolean false', () => {
|
||||
expect(booleanTransformer.toApp('false')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return undefined for null or undefined', () => {
|
||||
expect(booleanTransformer.toApp(null)).toBeUndefined();
|
||||
expect(booleanTransformer.toApp(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toStorage', () => {
|
||||
it('should keep boolean values unchanged', () => {
|
||||
expect(booleanTransformer.toStorage(true)).toBe(true);
|
||||
expect(booleanTransformer.toStorage(false)).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw error for non-boolean values', () => {
|
||||
expect(() => {
|
||||
booleanTransformer.toStorage('true' as any);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Number Transformer', () => {
|
||||
const numberTransformer = typeTransformers[ConfigVariableType.NUMBER];
|
||||
|
||||
describe('toApp', () => {
|
||||
it('should convert string number to number', () => {
|
||||
expect(numberTransformer.toApp('123')).toBe(123);
|
||||
});
|
||||
|
||||
it('should return undefined for non-parseable strings', () => {
|
||||
expect(numberTransformer.toApp('not-a-number')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined for null or undefined', () => {
|
||||
expect(numberTransformer.toApp(null)).toBeUndefined();
|
||||
expect(numberTransformer.toApp(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toStorage', () => {
|
||||
it('should keep number values unchanged', () => {
|
||||
expect(numberTransformer.toStorage(123)).toBe(123);
|
||||
});
|
||||
|
||||
it('should throw error for non-number values', () => {
|
||||
expect(() => {
|
||||
numberTransformer.toStorage('123' as any);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('String Transformer', () => {
|
||||
const stringTransformer = typeTransformers[ConfigVariableType.STRING];
|
||||
|
||||
describe('toApp', () => {
|
||||
it('should keep string values unchanged', () => {
|
||||
expect(stringTransformer.toApp('hello')).toBe('hello');
|
||||
});
|
||||
|
||||
it('should convert numbers to strings', () => {
|
||||
expect(stringTransformer.toApp(123)).toBe('123');
|
||||
});
|
||||
|
||||
it('should return undefined for null or undefined', () => {
|
||||
expect(stringTransformer.toApp(null)).toBeUndefined();
|
||||
expect(stringTransformer.toApp(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toStorage', () => {
|
||||
it('should keep string values unchanged', () => {
|
||||
expect(stringTransformer.toStorage('hello')).toBe('hello');
|
||||
});
|
||||
|
||||
it('should throw error for non-string values', () => {
|
||||
expect(() => {
|
||||
stringTransformer.toStorage(123 as any);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Array Transformer', () => {
|
||||
const arrayTransformer = typeTransformers[ConfigVariableType.ARRAY];
|
||||
|
||||
describe('toApp', () => {
|
||||
it('should keep array values unchanged', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
expect(arrayTransformer.toApp(array)).toEqual(array);
|
||||
});
|
||||
|
||||
it('should convert comma-separated string to array', () => {
|
||||
expect(arrayTransformer.toApp('a,b,c')).toEqual(['a', 'b', 'c']);
|
||||
});
|
||||
|
||||
it('should filter array values based on options', () => {
|
||||
expect(
|
||||
arrayTransformer.toApp(['a', 'b', 'c', 'd'], ['a', 'c']),
|
||||
).toEqual(['a', 'c']);
|
||||
});
|
||||
|
||||
it('should return undefined for null or undefined', () => {
|
||||
expect(arrayTransformer.toApp(null)).toBeUndefined();
|
||||
expect(arrayTransformer.toApp(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toStorage', () => {
|
||||
it('should keep array values unchanged', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
expect(arrayTransformer.toStorage(array)).toEqual(array);
|
||||
});
|
||||
|
||||
it('should filter array values based on options', () => {
|
||||
expect(
|
||||
arrayTransformer.toStorage(['a', 'b', 'c', 'd'], ['a', 'c']),
|
||||
).toEqual(['a', 'c']);
|
||||
});
|
||||
|
||||
it('should throw error for non-array values', () => {
|
||||
expect(() => {
|
||||
arrayTransformer.toStorage('not-an-array' as any);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Enum Transformer', () => {
|
||||
const enumTransformer = typeTransformers[ConfigVariableType.ENUM];
|
||||
const options = ['option1', 'option2', 'option3'];
|
||||
|
||||
describe('toApp', () => {
|
||||
it('should keep valid enum values unchanged', () => {
|
||||
expect(enumTransformer.toApp('option1', options)).toBe('option1');
|
||||
});
|
||||
|
||||
it('should return undefined for invalid enum values', () => {
|
||||
expect(
|
||||
enumTransformer.toApp('invalid-option', options),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return the value if no options are provided', () => {
|
||||
expect(enumTransformer.toApp('any-value')).toBe('any-value');
|
||||
});
|
||||
|
||||
it('should return undefined for null or undefined', () => {
|
||||
expect(enumTransformer.toApp(null)).toBeUndefined();
|
||||
expect(enumTransformer.toApp(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toStorage', () => {
|
||||
it('should keep valid enum values unchanged', () => {
|
||||
expect(enumTransformer.toStorage('option1', options)).toBe('option1');
|
||||
});
|
||||
|
||||
it('should throw error for invalid enum values', () => {
|
||||
expect(() => {
|
||||
enumTransformer.toStorage('invalid-option', options);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
it('should throw error for non-string values', () => {
|
||||
expect(() => {
|
||||
enumTransformer.toStorage(123 as any, options);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,19 +1,10 @@
|
||||
import { Transform } from 'class-transformer';
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
import { ConfigVariableType } from 'src/engine/core-modules/twenty-config/enums/config-variable-type.enum';
|
||||
import {
|
||||
ConfigVariableException,
|
||||
ConfigVariableExceptionCode,
|
||||
} from 'src/engine/core-modules/twenty-config/twenty-config.exception';
|
||||
import { ConfigVariableOptions } from 'src/engine/core-modules/twenty-config/types/config-variable-options.type';
|
||||
import { configTransformers } from 'src/engine/core-modules/twenty-config/utils/config-transformers.util';
|
||||
import { typeTransformers } from 'src/engine/core-modules/twenty-config/utils/type-transformers.registry';
|
||||
|
||||
export function applyBasicValidators(
|
||||
type: ConfigVariableType,
|
||||
@ -21,43 +12,20 @@ export function applyBasicValidators(
|
||||
propertyKey: string,
|
||||
options?: ConfigVariableOptions,
|
||||
): void {
|
||||
switch (type) {
|
||||
case ConfigVariableType.BOOLEAN:
|
||||
Transform(({ value }) => {
|
||||
const result = configTransformers.boolean(value);
|
||||
const transformer = typeTransformers[type];
|
||||
|
||||
return result !== undefined ? result : value;
|
||||
})(target, propertyKey);
|
||||
IsBoolean()(target, propertyKey);
|
||||
break;
|
||||
|
||||
case ConfigVariableType.NUMBER:
|
||||
Transform(({ value }) => {
|
||||
const result = configTransformers.number(value);
|
||||
|
||||
return result !== undefined ? result : value;
|
||||
})(target, propertyKey);
|
||||
IsNumber()(target, propertyKey);
|
||||
break;
|
||||
|
||||
case ConfigVariableType.STRING:
|
||||
IsString()(target, propertyKey);
|
||||
break;
|
||||
|
||||
case ConfigVariableType.ENUM:
|
||||
if (options) {
|
||||
IsEnum(options)(target, propertyKey);
|
||||
}
|
||||
break;
|
||||
|
||||
case ConfigVariableType.ARRAY:
|
||||
IsArray()(target, propertyKey);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ConfigVariableException(
|
||||
`Unsupported config variable type: ${type}`,
|
||||
ConfigVariableExceptionCode.UNSUPPORTED_CONFIG_TYPE,
|
||||
);
|
||||
if (!transformer) {
|
||||
throw new ConfigVariableException(
|
||||
`Unsupported config variable type: ${type}`,
|
||||
ConfigVariableExceptionCode.UNSUPPORTED_CONFIG_TYPE,
|
||||
);
|
||||
}
|
||||
|
||||
transformer
|
||||
.getTransformers()
|
||||
.forEach((decorator) => decorator(target, propertyKey));
|
||||
|
||||
transformer
|
||||
.getValidators(options)
|
||||
.forEach((decorator) => decorator(target, propertyKey));
|
||||
}
|
||||
|
||||
@ -0,0 +1,243 @@
|
||||
import { Transform } from 'class-transformer';
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
import { ConfigVariableType } from 'src/engine/core-modules/twenty-config/enums/config-variable-type.enum';
|
||||
import {
|
||||
ConfigVariableException,
|
||||
ConfigVariableExceptionCode,
|
||||
} from 'src/engine/core-modules/twenty-config/twenty-config.exception';
|
||||
import { ConfigVariableOptions } from 'src/engine/core-modules/twenty-config/types/config-variable-options.type';
|
||||
import { configTransformers } from 'src/engine/core-modules/twenty-config/utils/config-transformers.util';
|
||||
|
||||
export interface TypeTransformer<T> {
|
||||
toApp: (value: unknown, options?: ConfigVariableOptions) => T | undefined;
|
||||
|
||||
toStorage: (value: T, options?: ConfigVariableOptions) => unknown;
|
||||
|
||||
getValidators: (options?: ConfigVariableOptions) => PropertyDecorator[];
|
||||
|
||||
getTransformers: () => PropertyDecorator[];
|
||||
}
|
||||
|
||||
export const typeTransformers: Record<
|
||||
ConfigVariableType,
|
||||
TypeTransformer<any>
|
||||
> = {
|
||||
[ConfigVariableType.BOOLEAN]: {
|
||||
toApp: (value: unknown): boolean | undefined => {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
|
||||
const result = configTransformers.boolean(value);
|
||||
|
||||
if (result !== undefined && typeof result !== 'boolean') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected boolean, got ${typeof result}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
toStorage: (value: boolean): boolean => {
|
||||
if (typeof value !== 'boolean') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected boolean, got ${typeof value}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
getValidators: (): PropertyDecorator[] => [IsBoolean()],
|
||||
|
||||
getTransformers: (): PropertyDecorator[] => [
|
||||
Transform(({ value }) => {
|
||||
const result = configTransformers.boolean(value);
|
||||
|
||||
return result !== undefined ? result : value;
|
||||
}),
|
||||
],
|
||||
},
|
||||
|
||||
[ConfigVariableType.NUMBER]: {
|
||||
toApp: (value: unknown): number | undefined => {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
|
||||
const result = configTransformers.number(value);
|
||||
|
||||
if (result !== undefined && typeof result !== 'number') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected number, got ${typeof result}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
toStorage: (value: number): number => {
|
||||
if (typeof value !== 'number') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected number, got ${typeof value}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
getValidators: (): PropertyDecorator[] => [IsNumber()],
|
||||
|
||||
getTransformers: (): PropertyDecorator[] => [
|
||||
Transform(({ value }) => {
|
||||
const result = configTransformers.number(value);
|
||||
|
||||
return result !== undefined ? result : value;
|
||||
}),
|
||||
],
|
||||
},
|
||||
|
||||
[ConfigVariableType.STRING]: {
|
||||
toApp: (value: unknown): string | undefined => {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
|
||||
const result = configTransformers.string(value);
|
||||
|
||||
if (result !== undefined && typeof result !== 'string') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected string, got ${typeof result}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
toStorage: (value: string): string => {
|
||||
if (typeof value !== 'string') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected string, got ${typeof value}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
getValidators: (): PropertyDecorator[] => [IsString()],
|
||||
|
||||
getTransformers: (): PropertyDecorator[] => [],
|
||||
},
|
||||
|
||||
[ConfigVariableType.ARRAY]: {
|
||||
toApp: (
|
||||
value: unknown,
|
||||
options?: ConfigVariableOptions,
|
||||
): unknown[] | undefined => {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
|
||||
let arrayValue: unknown[];
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
arrayValue = value;
|
||||
} else if (typeof value === 'string') {
|
||||
try {
|
||||
const parsedArray = JSON.parse(value);
|
||||
|
||||
if (Array.isArray(parsedArray)) {
|
||||
arrayValue = parsedArray;
|
||||
} else {
|
||||
arrayValue = value.split(',').map((item) => item.trim());
|
||||
}
|
||||
} catch {
|
||||
arrayValue = value.split(',').map((item) => item.trim());
|
||||
}
|
||||
} else {
|
||||
arrayValue = [value];
|
||||
}
|
||||
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return arrayValue;
|
||||
}
|
||||
|
||||
return arrayValue.filter((item) => options.includes(item as string));
|
||||
},
|
||||
|
||||
toStorage: (
|
||||
value: unknown[],
|
||||
options?: ConfigVariableOptions,
|
||||
): unknown[] => {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new ConfigVariableException(
|
||||
`Expected array, got ${typeof value}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return value.filter((item) => options.includes(item as string));
|
||||
},
|
||||
|
||||
getValidators: (): PropertyDecorator[] => [IsArray()],
|
||||
|
||||
getTransformers: (): PropertyDecorator[] => [],
|
||||
},
|
||||
|
||||
[ConfigVariableType.ENUM]: {
|
||||
toApp: (
|
||||
value: unknown,
|
||||
options?: ConfigVariableOptions,
|
||||
): string | undefined => {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return value as string;
|
||||
}
|
||||
|
||||
return options.includes(value as string) ? (value as string) : undefined;
|
||||
},
|
||||
|
||||
toStorage: (value: string, options?: ConfigVariableOptions): string => {
|
||||
if (typeof value !== 'string') {
|
||||
throw new ConfigVariableException(
|
||||
`Expected string for enum, got ${typeof value}`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
if (!options || !Array.isArray(options) || options.length === 0) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!options.includes(value)) {
|
||||
throw new ConfigVariableException(
|
||||
`Value '${value}' is not a valid option for enum`,
|
||||
ConfigVariableExceptionCode.VALIDATION_FAILED,
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
getValidators: (options?: ConfigVariableOptions): PropertyDecorator[] => {
|
||||
if (!options) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [IsEnum(options)];
|
||||
},
|
||||
|
||||
getTransformers: (): PropertyDecorator[] => [],
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user