@trenskow/config 2.0.0 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,11 @@
12
12
  "<node_internals>/**"
13
13
  ],
14
14
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
15
- "args": ["--bail"]
15
+ "args": [
16
+ "--bail",
17
+ "--timeout",
18
+ "0"
19
+ ]
16
20
  }
17
21
  ]
18
- }
22
+ }
package/index.js CHANGED
@@ -9,7 +9,7 @@ const
9
9
  let config = {};
10
10
 
11
11
  // We first split the env names at `_` and put them into objects.
12
- Object.keys(process.env)
12
+ Object.keys(process.env).filter((key) => process.env[key])
13
13
  .forEach((key) => {
14
14
 
15
15
  // Split and filter.
@@ -35,7 +35,7 @@ Object.keys(process.env)
35
35
 
36
36
  const fillArray = (value, length) => {
37
37
  let result = [];
38
- for (let idx = 0 ; idx < length ; idx++) result.push(value);
38
+ for (let idx = 0; idx < length; idx++) result.push(value);
39
39
  return result;
40
40
  };
41
41
 
@@ -45,7 +45,7 @@ const makeArrayIfNecessary = (obj) => {
45
45
  const arrayKeys = fillArray(undefined, keys.length)
46
46
  .map((_, idx) => idx);
47
47
 
48
- for (let idx = 0 ; idx < keys.length; idx++) {
48
+ for (let idx = 0; idx < keys.length; idx++) {
49
49
  if (keys[idx] != arrayKeys[idx]) return obj;
50
50
  }
51
51
 
@@ -57,12 +57,12 @@ const makeArrayIfNecessary = (obj) => {
57
57
  const cleanIt = (obj, expanded = [], keyPath = []) => {
58
58
 
59
59
  if (typeof obj === 'undefined') return;
60
-
60
+
61
61
  if (!Array.isArray(expanded)) expanded = expanded.split(/, ?/);
62
62
 
63
63
  // If the object is a string we just return it.
64
64
  if (typeof obj === 'string') return obj;
65
-
65
+
66
66
  // Now clean all the keys.
67
67
  obj = Object.keys(obj).reduce((res, key) => {
68
68
  res[key.toLowerCase()] = cleanIt(obj[key], expanded, keyPath.concat([key]));
@@ -73,8 +73,14 @@ const cleanIt = (obj, expanded = [], keyPath = []) => {
73
73
  obj = Object.keys(obj).reduce((res, key) => {
74
74
 
75
75
  const keys = obj[key] ? Object.keys(obj[key]) : [];
76
- const fullKeyPath = keyd.append(keyd.join(keyPath.map((key) => key.toLowerCase())), key);
77
- const isLocked = expanded.some((keyPath) => keyd.within(fullKeyPath, keyPath));
76
+
77
+ let fullKeyPath = keyd.join(keyPath.map((key) => key.toLowerCase()));
78
+
79
+ if (key !== '$') fullKeyPath = keyd.append(fullKeyPath, key);
80
+
81
+ const isLocked = expanded.some((keyPath) => {
82
+ return keyPath.split(/(?=[A-Z])|\./).map((key) => caseit(key)).join('.') == fullKeyPath;
83
+ });
78
84
 
79
85
  if (Array.isArray(obj[key]) || isLocked || typeof obj[key] === 'undefined' || typeof obj[key] === 'string' || typeof obj[key] === 'number' || keys.length > 1) {
80
86
  res[key] = obj[key];
@@ -96,23 +102,25 @@ module.exports = cleanIt(config);
96
102
 
97
103
  module.exports.validate = async (schema, options = {}) => {
98
104
 
99
- module.exports = merge(module.exports, cleanIt(config, isvalid.keyPaths(schema, Object).filter((keyPath) => keyPath)), {
105
+ schema = isvalid.formalize(schema);
106
+
107
+ module.exports = merge(module.exports, cleanIt(config, isvalid.keyPaths(schema).all(Object).filter((keyPath) => keyPath)), {
100
108
  validate: module.exports.validate
101
109
  });
102
110
 
103
111
  options = merge({
104
112
  defaults: {
105
113
  unknownKeys: 'allow'
106
- },
107
- throwDeepKeyOnImplicit: true
114
+ }
108
115
  }, options);
109
116
 
110
117
  try {
111
118
  return module.exports = merge(module.exports, await isvalid(
112
119
  module.exports,
113
- merge(schema, {
120
+ isvalid.merge(schema).with({
114
121
  'validate': {
115
- type: 'AsyncFunction'
122
+ type: 'AsyncFunction',
123
+ required: true
116
124
  }
117
125
  }),
118
126
  options));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/config",
3
- "version": "2.0.0",
3
+ "version": "2.1.5",
4
4
  "description": "Converts `process.env` into a neatly packed object.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,8 +26,8 @@
26
26
  "homepage": "https://github.com/trenskow/config#readme",
27
27
  "dependencies": {
28
28
  "@trenskow/caseit": "^1.0.1",
29
- "isvalid": "^2.8.0",
30
- "keyd": "^1.4.0",
29
+ "isvalid": "^2.10.3",
30
+ "keyd": "^1.4.2",
31
31
  "merge": "^2.1.1"
32
32
  },
33
33
  "devDependencies": {
package/test/index.js CHANGED
@@ -13,6 +13,7 @@ process.env = {
13
13
  CONFIG_TEST_ARRAY_1: '1',
14
14
  CONFIG_TEST_ARRAY_2: '2',
15
15
  CONFIG_TEST_ARRAY_3: '3',
16
+ CONFIG_TEST_THIS_IS_A_DEEPLY_NESTED_KEY: 'true',
16
17
  config_test_small_array_0: '0'
17
18
  };
18
19
 
@@ -30,7 +31,7 @@ describe('config', () => {
30
31
  config.validate({
31
32
  'configTest': {
32
33
  'obj': {
33
- 'string': { type: Number, required: true}
34
+ 'string': { type: Number, required: true }
34
35
  }
35
36
  }
36
37
  }).then(() => {
@@ -93,4 +94,25 @@ describe('config', () => {
93
94
  done();
94
95
  }).catch(done);
95
96
  });
97
+ it ('must validate a deeply nested key.', (done) => {
98
+ config.validate({
99
+ 'configTest': {
100
+ 'this': {
101
+ 'is': {
102
+ 'a': {
103
+ 'deeply': {
104
+ 'nested': {
105
+ 'key': { type: Boolean }
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }).then(() => {
113
+ const result = require('../');
114
+ expect(result.configTest.this.is.a.deeply.nested.key).to.equal(true);
115
+ done();
116
+ }).catch(done);
117
+ });
96
118
  });