diff --git a/src/config/config.ts b/src/config/config.ts index 850aea668..522fc8f67 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -11,13 +11,19 @@ convict.addFormat({ convict.addFormat({ name: "required-positive-number", validate: (val: any) => { - if (!val) throw new Error("value cannot be empty, null or undefined") + if (val === null || val === undefined || val === "") + throw new Error("value cannot be empty, null or undefined") if (typeof val !== "number") throw new Error("value must be a number") }, coerce: (val: string) => { const coercedVal = Number(val) - if (coercedVal < 0) { - throw new Error("value must be a positive number") + if (isNaN(coercedVal)) { + throw new Error( + "value provided is not a positive number. please provide a valid positive number" + ) + } + if (coercedVal <= 0) { + throw new Error("value must be more than zero") } return coercedVal }, @@ -331,7 +337,12 @@ const config = convict({ }) // Perform validation -config.validate({ allowed: "strict" }) +// TODO: remove try-catch after prod deployment is successful to avoid blocking +try { + config.validate({ allowed: "strict" }) +} catch (e: any) { + console.log(`Convict error: ${e}`) +} export default config export { config } diff --git a/src/database/config.js b/src/database/config.js index 045fea9a5..43ded8222 100644 --- a/src/database/config.js +++ b/src/database/config.js @@ -1,14 +1,15 @@ const { parse } = require("pg-connection-string") -const { config } = require("@config/config") - // TODO: This came from a past project - I don't remember why I wrote this but let's explore later. // We have to manually parse database URL because sequelize-typescript requires explicit // connection parameters. -const DB_URI = config.get("database.dbUri") -const DB_MIN_POOL = config.get("database.dbMinPool") -const DB_MAX_POOL = config.get("database.dbMaxPool") +// Note: We are using process.env here instead of convict's config.get() as sequelize-cli is unable +// to support import of TS files inside JS. Note that validation of these envs will still be +// performed by convict in src/config/config.ts. +const { DB_URI } = process.env +const DB_MIN_POOL = parseInt(process.env.DB_MIN_POOL, 10) +const DB_MAX_POOL = parseInt(process.env.DB_MAX_POOL, 10) const parsed = parse(DB_URI) const port = parsed.port ? parseInt(parsed.port, 10) : 5432