forked from MandarinConLaBarba/envok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (29 loc) · 824 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict'
const Joi = require('joi'),
_ = require('underscore'),
camelize = require('camelize');
const _check = (schema) => {
const env = process.env,
envKeys = Object.keys(schema),
envCfg = _.pick(env, envKeys);
let validatedCfg = {};
envKeys.forEach((k) => {
const singleVarSchema = Joi.object({
[k]: schema[k]
});
const singleVarCfg = {
[k]: envCfg[k]
};
let result = singleVarSchema.validate(singleVarCfg);
if (result.error) {
throw new Error("Environment var validation exception: " + result.error);
}
validatedCfg = { ...validatedCfg, ...result.value }
})
let formattedCfg = {};
envKeys.forEach((k, v) => {
formattedCfg[camelize(k.toLowerCase())] = validatedCfg[k];
});
return formattedCfg
}
module.exports = _check;