-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Merge pull request #209 from pelias/add-config-validation
added configuration validation with tests
- Loading branch information
Showing
6 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,26 @@ | ||
'use strict'; | ||
|
||
const Joi = require('joi'); | ||
|
||
// Schema Configuration | ||
// datapath: string (required) | ||
// files: array of strings | ||
// deduplicate: boolean | ||
// adminLookup: boolean | ||
const schema = Joi.object().keys({ | ||
files: Joi.array().items(Joi.string()), | ||
datapath: Joi.string(), | ||
deduplicate: Joi.boolean(), | ||
adminLookup: Joi.boolean() | ||
}).requiredKeys('datapath').unknown(false); | ||
|
||
module.exports = { | ||
validate: function validate(config) { | ||
Joi.validate(config, schema, (err) => { | ||
if (err) { | ||
throw new Error(err.details[0].message); | ||
} | ||
}); | ||
} | ||
|
||
}; |
This file contains 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 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 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,130 @@ | ||
'use strict'; | ||
|
||
const tape = require( 'tape' ); | ||
|
||
const configValidation = require( '../configValidation' ); | ||
|
||
tape( 'missing datapath should throw error', function(test) { | ||
const config = {}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"datapath" is required/); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'non-string datapath should throw error', function(test) { | ||
[null, 17, {}, [], false].forEach((value) => { | ||
const config = { | ||
datapath: value | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"datapath" must be a string/); | ||
|
||
}); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'non-array files should throw error', function(test) { | ||
[null, 17, {}, 'string', false].forEach((value) => { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
files: value | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"files" must be an array/, 'datapath is required'); | ||
}); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'non-string elements in files array should throw error', function(test) { | ||
[null, 17, {}, [], false].forEach((value) => { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
files: [value] | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"0" must be a string/, 'files elements must be strings'); | ||
}); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'non-boolean adminLookup should throw error', function(test) { | ||
[null, 17, {}, [], 'string'].forEach((value) => { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
adminLookup: value | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"adminLookup" must be a boolean/); | ||
}); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'non-boolean deduplicate should throw error', function(test) { | ||
[null, 17, {}, [], 'string'].forEach((value) => { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
deduplicate: value | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"deduplicate" must be a boolean/); | ||
}); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape( 'unknown config fields should throw error', function(test) { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
unknown: 'value' | ||
}; | ||
|
||
test.throws(() => { | ||
configValidation.validate(config); | ||
}, /"unknown" is not allowed/, 'unknown fields should be disallowed'); | ||
test.end(); | ||
|
||
}); | ||
|
||
tape( 'configuration with only datapath should not throw error', function(test) { | ||
const config = { | ||
datapath: 'this is the datapath' | ||
}; | ||
|
||
test.doesNotThrow(() => { | ||
configValidation.validate(config); | ||
}, 'config should be valid'); | ||
test.end(); | ||
|
||
}); | ||
|
||
tape( 'valid configuration should not throw error', function(test) { | ||
const config = { | ||
datapath: 'this is the datapath', | ||
deduplicate: false, | ||
adminLookup: false, | ||
files: ['file 1', 'file 2'] | ||
}; | ||
|
||
test.doesNotThrow(() => { | ||
configValidation.validate(config); | ||
}, 'config should be valid'); | ||
test.end(); | ||
|
||
}); |
This file contains 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,21 @@ | ||
'use strict'; | ||
|
||
const tape = require( 'tape' ); | ||
|
||
const proxyquire = require('proxyquire').noCallThru(); | ||
|
||
tape( 'configValidation throwing error should rethrow', function(test) { | ||
test.throws(function() { | ||
proxyquire('../import', { | ||
'./configValidation': { | ||
validate: () => { | ||
throw Error('config is not valid'); | ||
} | ||
} | ||
})(); | ||
|
||
}, /config is not valid/); | ||
|
||
test.end(); | ||
|
||
}); |
This file contains 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