Skip to content

Commit b8134b6

Browse files
Fix for redis config object/string error.
1 parent 1fc194e commit b8134b6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/common/configs/config.helper.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ function parseNestedJson(config: any): ApplicationConfig {
4747
* including the applied default values.
4848
*/
4949
function validateConfig(envConfig: object): ApplicationConfig {
50+
const validatedConfig: ApplicationConfig = { ...envConfig } as ApplicationConfig;
51+
52+
// Validate REDIS_SERVERS as JSON
53+
if (typeof validatedConfig.REDIS_SERVERS === 'string') {
54+
try {
55+
validatedConfig.REDIS_SERVERS = JSON.parse(validatedConfig.REDIS_SERVERS);
56+
} catch (error) {
57+
throw new Error(`Config validation error: REDIS_SERVERS must be a valid JSON string`);
58+
}
59+
}
60+
61+
// Ensure REDIS_SERVERS is an object after parsing
62+
if (typeof validatedConfig.REDIS_SERVERS !== 'object' || validatedConfig.REDIS_SERVERS === null) {
63+
throw new Error(`Config validation error: REDIS_SERVERS must be a valid object`);
64+
}
65+
5066
const envVarsSchema: Joi.ObjectSchema<ApplicationConfig> = Joi.object<ApplicationConfig>({
5167
SERVICE_NAME: Joi.string().required(),
5268
SERVICE_VERSION: Joi.string().required(),
@@ -88,16 +104,16 @@ function validateConfig(envConfig: object): ApplicationConfig {
88104

89105
EDGE_KV_URL: Joi.string().default('https://edge-kv-dexcelerate.workers.dev'),
90106
EDGE_KV_AUTHORIZATION_TOKEN: Joi.string().default(''),
91-
REDIS_SERVERS: Joi.string().required(), // JSON string validated as plain string here
107+
REDIS_SERVERS: Joi.object().required(), // Validate as an object
92108
});
93109

94-
const { error, value: validatedConfig } = envVarsSchema.validate(envConfig, {
110+
const { error, value: configValue } = envVarsSchema.validate(validatedConfig, {
95111
allowUnknown: true,
96112
});
97113

98114
if (error) {
99115
throw new Error(`Config validation error: ${error.message}`);
100116
}
101117

102-
return validatedConfig;
118+
return configValue;
103119
}

0 commit comments

Comments
 (0)