Skip to content

Commit db9247c

Browse files
committed
Add conf.toJson() method
1 parent 9ca2d82 commit db9247c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/convict/src/main.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,10 @@ const convict = function convict(def, opts) {
484484

485485
/**
486486
* Exports all the properties (that is the keys and their current values) as
487-
* a JSON string, with sensitive values masked. Sensitive values are masked
487+
* a JSON object, with sensitive values masked. Sensitive values are masked
488488
* even if they aren't set, to avoid revealing any information.
489489
*/
490-
toString: function() {
490+
toJson: function() {
491491
const clone = cloneDeep(this._instance)
492492
this._sensitive.forEach(function(key) {
493493
const path = key.split('.')
@@ -496,7 +496,16 @@ const convict = function convict(def, opts) {
496496
const parent = walk(clone, parentKey)
497497
parent[childKey] = '[Sensitive]'
498498
})
499-
return JSON.stringify(clone, null, 2)
499+
return clone
500+
},
501+
502+
/**
503+
* Exports all the properties (that is the keys and their current values) as
504+
* a JSON string, with sensitive values masked. Sensitive values are masked
505+
* even if they aren't set, to avoid revealing any information.
506+
*/
507+
toString: function() {
508+
return JSON.stringify(this.toJson(), null, 2)
500509
},
501510

502511
/**

packages/convict/test/schema.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ describe('convict schema', function() {
102102
}, null, 2))
103103
})
104104

105+
test('must export all its properties as a json object', function() {
106+
const res = conf.toJson()
107+
expect(res).toEqual({
108+
foo: {
109+
bar: 7,
110+
baz: {
111+
bing: 'foo',
112+
'name with spaces': {
113+
name_with_underscores: true
114+
}
115+
}
116+
}
117+
})
118+
})
119+
105120
test('must throw if `_cvtProperties` (reserved keyword) is used', function() {
106121
expect(function() {
107122
conf = convict({

0 commit comments

Comments
 (0)