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 conf.toJson() method #407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/convict/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ convict's goal of being more robust and collaborator friendly.
* **Environmental variables**: If the variable specified by `env` has a value, it will overwrite the setting's default value. An environment variable may not be mapped to more than one setting.
* **Command-line arguments**: If the command-line argument specified by `arg` is supplied, it will overwrite the setting's default value or the value derived from `env`.
* **Documentation**: The `doc` property is pretty self-explanatory. The nice part about having it in the schema rather than as a comment is that we can call `config.getSchemaString()` and have it displayed in the output.
* **Sensitive values and secrets**: If `sensitive` is set to `true`, this value will be masked to `"[Sensitive]"` when `config.toString()` is called. This helps avoid disclosing secret keys when printing configuration at application start for debugging purposes.
* **Sensitive values and secrets**: If `sensitive` is set to `true`, this value will be masked to `"[Sensitive]"` when `config.toString()` or `config.toJson()` is called. This helps avoid disclosing secret keys when printing configuration at application start for debugging purposes.
* **Null values**: If `nullable` is set to `true`, the value counts as valid not only if it matches the specified `format`, but also when it is `null`.


Expand Down Expand Up @@ -585,6 +585,12 @@ collected and thrown or displayed at once.

Exports all the properties (that is the keys and their current values) as JSON.

### config.toJson()

Exports all the properties (that is the keys and their current values) as a JSON
object, with sensitive values masked. Sensitive values are masked even if they
aren't set, to avoid revealing any information.

### config.toString()

Exports all the properties (that is the keys and their current values) as a JSON
Expand Down
15 changes: 12 additions & 3 deletions packages/convict/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ const convict = function convict(def, opts) {

/**
* Exports all the properties (that is the keys and their current values) as
* a JSON string, with sensitive values masked. Sensitive values are masked
* a JSON object, with sensitive values masked. Sensitive values are masked
* even if they aren't set, to avoid revealing any information.
*/
toString: function() {
toJson: function() {
const clone = cloneDeep(this._instance)
this._sensitive.forEach(function(key) {
const path = key.split('.')
Expand All @@ -496,7 +496,16 @@ const convict = function convict(def, opts) {
const parent = walk(clone, parentKey)
parent[childKey] = '[Sensitive]'
})
return JSON.stringify(clone, null, 2)
return clone
},

/**
* Exports all the properties (that is the keys and their current values) as
* a JSON string, with sensitive values masked. Sensitive values are masked
* even if they aren't set, to avoid revealing any information.
*/
toString: function() {
return JSON.stringify(this.toJson(), null, 2)
},

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/convict/test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ describe('convict schema', function() {
}, null, 2))
})

test('must export all its properties as a json object', function() {
const res = conf.toJson()
expect(res).toEqual({
foo: {
bar: 7,
baz: {
bing: 'foo',
'name with spaces': {
name_with_underscores: true
}
}
}
})
})

test('must throw if `_cvtProperties` (reserved keyword) is used', function() {
expect(function() {
conf = convict({
Expand Down