Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment checker #13

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add validation for nested values
mamaz committed Jan 2, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 096967fa53bff6f998e3790f866c8ac9b19a92b5
3 changes: 1 addition & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -60,7 +60,6 @@ class Config {
}
data = val;
}

return data;
}
/**
@@ -192,7 +191,6 @@ class Config {
}
return tpl(params);
}

return tpl(params);
}

@@ -226,6 +224,7 @@ class Config {
for (let n in flat) {
ret[n] = this.set(n, this.resolve(n, false));
}

return ret;
}

20 changes: 17 additions & 3 deletions lib/env_validator.js
Original file line number Diff line number Diff line change
@@ -39,10 +39,10 @@ exports.validateEnvironment = (environment, configuration, delimiters = { left:
const envValue = environment[envKey];
const sanitisedEnvKey = envKey.replace(/\$/,""); // remove $

if (isNil(envValue)) {
neededValues["undefined"].push(sanitisedEnvKey);
} else if (envValue === "") {
if (envValue === "") {
neededValues["empty"].push(sanitisedEnvKey);
} else if (isNil(envValue) && !isNestedValue(envKey, configuration)) {
neededValues["undefined"].push(sanitisedEnvKey);
}
}
}
@@ -57,3 +57,17 @@ const containDelimiters = (string, delimiters) => {
string.includes(delimiters.right);
};
exports.containDelimiters = containDelimiters;

const isNestedValue = (value, config) => {
let data = config;
const parts = value.split(".").map(val => /^\d+$/.test(val) ? parseInt(val) : val);
if (parts.length === 1) return false;
for(let i = 0; i < parts.length; i++) {
let value = data[parts[i]];
if (data === undefined) {
return false;
}
data = value;
}
return true;
};
8 changes: 5 additions & 3 deletions test/merapi_test.spec.js
Original file line number Diff line number Diff line change
@@ -29,13 +29,15 @@ describe("Merapi Test", function() {
"myEnvConfig": "${resolved.b}",
"myStrEnvConfig": "${resolved.c}",
"myCrlfStrEnvConfig": "${resolved.d}",
"myToken": "${$TOKEN}" // for system env variables, $ is appended
"myToken": "${$TOKEN}", // for system env variables, $ is appended
"resolved": {
"a": 1
}
},

envConfig: {
test: {
"resolved.a": 1,
"resolved.b": 2,
"resolved.b": 2,
"resolved.c": "test",
"resolved.d": "test\r",
}