Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Validate config (#74)
Browse files Browse the repository at this point in the history
* Validate config

* Refactor function

* Handle invalid values in arrays

* kebab-case that file
  • Loading branch information
MrGuzior authored Jul 30, 2024
1 parent 89a18f8 commit 34c67c4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/build-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import statusRoutes from "./routes.js";
import { initHandleSigterm } from "./handle-sigterm.js";
import notFoundHandler from "./notFoundHandler.js";
import errorHandler from "./errorHandler.js";
import validateConfig from "./config-validator.js";

export default function buildApp(routes) {
validateConfig(config);

const app = express();

app.disable("x-powered-by");
Expand Down
31 changes: 31 additions & 0 deletions lib/config-validator.js
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;
}
48 changes: 48 additions & 0 deletions test/lib/config-handler-test.js
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");
});
});

0 comments on commit 34c67c4

Please sign in to comment.