This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Validate config * Refactor function * Handle invalid values in arrays * kebab-case that file
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default function validateConfig(config) { | ||
const containsInvalidValues = JSON.stringify(config).includes("REPLACED_BY_ENV"); | ||
|
||
if (containsInvalidValues) { | ||
const flattenedConfig = flattenObject(config); | ||
const invalidValues = Object.keys(flattenedConfig).filter((key) => { | ||
return flattenedConfig[key] === "REPLACED_BY_ENV"; | ||
}); | ||
|
||
if (invalidValues.length !== 0) { | ||
throw new Error(`Invalid config for keys: ${invalidValues.join(", ")}`); | ||
} | ||
|
||
throw new Error("Config contains invalid values"); | ||
} | ||
} | ||
|
||
function flattenObject(o) { | ||
const result = {}; | ||
for (const i in o) { | ||
if ((typeof o[i]) === "object" && !Array.isArray(o[i])) { | ||
const fo = flattenObject(o[i]); | ||
for (const j in fo) { | ||
result[`${i}.${j}`] = fo[j]; | ||
} | ||
} else { | ||
result[i] = o[i]; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect } from "chai"; | ||
|
||
import validateConfig from "../../lib/config-validator.js"; | ||
|
||
describe("Config validator", () => { | ||
it("should not return anything", () => { | ||
const config = { | ||
envName: "test", | ||
logClientIp: true, | ||
express: { body: { limit: "200kb" } }, | ||
}; | ||
should.not.exist(validateConfig(config)); | ||
}); | ||
|
||
it("should throw error", () => { | ||
const config = { | ||
envName: "test", | ||
logClientIp: true, | ||
express: { body: { limit: "REPLACED_BY_ENV" } }, | ||
}; | ||
expect(() => validateConfig(config)).to.throw("Invalid config for keys: express.body.limit"); | ||
}); | ||
|
||
it("should throw error", () => { | ||
const config = { | ||
envName: "test", | ||
logClientIp: true, | ||
express: { body: { limit: "200kb" } }, | ||
items: [ | ||
{ name: "REPLACED_BY_ENV" }, | ||
{ | ||
name: "yello", | ||
value: "REPLACED_BY_ENV", | ||
}, | ||
], | ||
}; | ||
expect(() => validateConfig(config)).to.throw("Config contains invalid values"); | ||
}); | ||
|
||
it("should throw error", () => { | ||
const config = { | ||
envName: "test", | ||
logClientIp: "REPLACED_BY_ENV", | ||
express: { body: { limit: "REPLACED_BY_ENV" } }, | ||
}; | ||
expect(() => validateConfig(config)).to.throw("Invalid config for keys: logClientIp, express.body.limit"); | ||
}); | ||
}); |