Skip to content

Commit

Permalink
Fix/convict fixes (#663)
Browse files Browse the repository at this point in the history
* fix: update database config to use process.env

* feat: strengthen validation for number check

* fix: add try-catch to convict validate to prevent hard failure on deployment

* fix: rename to use whole number

* fix: console log convict err

* fix: update check for whole to natural number

* fix: rename back to positive number

* fix: add radix param
  • Loading branch information
harishv7 authored Mar 28, 2023
1 parent bc12469 commit eb89781
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
19 changes: 15 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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 }
11 changes: 6 additions & 5 deletions src/database/config.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit eb89781

Please sign in to comment.