-
Notifications
You must be signed in to change notification settings - Fork 12
Feature/add config validation #15
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
Open
Daniel1984
wants to merge
13
commits into
bitfinexcom:master
Choose a base branch
from
Daniel1984:feature/add-config-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e5f9519
adding check for missing key/value pairs from config example
Daniel1984 26081f5
adding tests and linter
Daniel1984 851af6f
removed redundant comment
Daniel1984 49f8c9a
deep comparison and optional validation
Daniel1984 183c1c0
updating readme
Daniel1984 dd5dbf7
mc
Daniel1984 2a08e80
renaming
Daniel1984 f2a3a3b
Update base.js
Daniel1984 e3dda1f
Update test/base.unit.test.js
Daniel1984 1ab2cdd
pr feedback
Daniel1984 0d5c126
making sure to avoid arrays in recursion
Daniel1984 f348d02
smarter required check
Daniel1984 95488fa
more pr feedback changes
Daniel1984 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,27 @@ git remote add upstream [email protected]:bitfinexcom/PARENT.git | |
| git remote -v | ||
|
|
||
| git push origin master | ||
|
|
||
| ## Config validation | ||
|
|
||
| If config file has corresponding `.example` file, by default `loadConf` will run a check for missing keys. | ||
|
|
||
| If config is missing some keys that are present in `.example` file, program will exit and report all missing keys. | ||
|
|
||
| `loadConf` accepts 3rd, optional validation object parameter: | ||
|
|
||
| ```js | ||
| this.loadConf("cosmos.coin", "coin", { | ||
| symbol: { | ||
| required: true, | ||
| sameAsExample: true, | ||
Daniel1984 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| "some.nested.value": { | ||
| required: true, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Keys of this validation object are used to access config values using lodash `_.get` so you can | ||
| chain access pattern as in example above `some.nested.value`. You can specify here if the value is | ||
| `required: true` and if value should be equal to corresponding value from `.example` file `sameAsExample: true`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,5 +30,19 @@ | |
| "name": "prdn", | ||
| "email": "[email protected]" | ||
| } | ||
| ] | ||
| ], | ||
| "devDependencies": { | ||
| "mocha": "^10.1.0", | ||
| "standard": "16.0.0", | ||
| "sinon": "^14.0.1" | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha ./test --exit", | ||
| "lint": "standard" | ||
| }, | ||
| "standard": { | ||
| "env": [ | ||
| "mocha" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| 'use strict' | ||
|
|
||
| /* eslint-env mocha */ | ||
|
|
||
| const assert = require('assert') | ||
| const sinon = require('sinon') | ||
| const WrkBase = require('../base') | ||
| const validConf = require('./config/valid.coin') | ||
|
|
||
| describe('loadConf', () => { | ||
| beforeEach(() => { | ||
| sinon.stub(process, 'exit') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| process.exit.restore() | ||
| }) | ||
|
|
||
| it('should exit when config keys do not match example config keys', () => { | ||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('missing-keys.coin', 'coin') | ||
| assert.ok(process.exit.calledOnceWithExactly(1)) | ||
| }) | ||
|
|
||
| it('should exit when config keys do not match example nested config keys', () => { | ||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('missing-nested-keys.coin', 'coin') | ||
| assert.ok(process.exit.calledOnceWithExactly(1)) | ||
| }) | ||
|
|
||
| it('should exit if required value is missing', () => { | ||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('missing-values.coin', 'coin', { symbol: { required: true } }) | ||
| assert.ok(process.exit.calledOnceWithExactly(1)) | ||
| }) | ||
|
|
||
| it('should exit if cfg value not equal example value', () => { | ||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('value-mismatch.coin', 'coin', { sweepKeep: { sameAsExample: true } }) | ||
| assert.ok(process.exit.calledOnceWithExactly(1)) | ||
| }) | ||
|
|
||
| it('should exit if nested cfg value not equal nested example value', () => { | ||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('nested-value-mismatch.coin', 'coin', { 'root.lvl2.lvl3.value': { sameAsExample: true } }) | ||
| assert.ok(process.exit.calledOnceWithExactly(1)) | ||
| }) | ||
|
|
||
| it('should not exit when config keys match example config keys', () => { | ||
Daniel1984 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const workerBase = new WrkBase({}, { root: __dirname }) | ||
| workerBase.loadConf('valid.coin', 'coin', { | ||
| 'root.lvl2.lvl3.value': { sameAsExample: true, required: true }, | ||
| fee: { required: true } | ||
| }) | ||
|
|
||
| assert.equal(process.exit.called, false) | ||
| assert.equal(process.exit.calledWith(1), false) | ||
| assert.deepStrictEqual(workerBase.conf.coin, validConf) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "sweepKeep": 0.01 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": {} | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": { | ||
| "value": 1 | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.001, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": { | ||
| "value": [ | ||
| 4, | ||
| 5, | ||
| 6 | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.001, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": { | ||
| "value": [1, 2, 3] | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01, | ||
| "fee": 0, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": { | ||
| "value": [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.01, | ||
| "fee": 0, | ||
| "root": { | ||
| "lvl2": { | ||
| "lvl3": { | ||
| "value": [1, 2, 3] | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 1000 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "provider": "http://127.0.0.1:8080", | ||
| "currency": "TEST", | ||
| "symbol": "TEST", | ||
| "decimals": 8, | ||
| "sweepKeep": 0.001 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.