-
-
Notifications
You must be signed in to change notification settings - Fork 77
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 #199 from pelias/add-config-validation
added validation for configuration
- Loading branch information
Showing
6 changed files
with
153 additions
and
1 deletion.
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,24 @@ | ||
'use strict'; | ||
|
||
const Joi = require('joi'); | ||
|
||
// Schema Configuration | ||
// schema.indexName: populated by defaults if not overridden | ||
// esclient: object, validation performed by elasticsearch module | ||
const schema = Joi.object().keys({ | ||
schema: { | ||
indexName: Joi.string() | ||
}, | ||
esclient: Joi.object() | ||
}).requiredKeys('schema', 'schema.indexName', 'esclient').unknown(true); | ||
|
||
module.exports = { | ||
validate: function validate(config) { | ||
Joi.validate(config, schema, (err, value) => { | ||
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,100 @@ | ||
'use strict'; | ||
|
||
const tape = require('tape'); | ||
|
||
const configValidation = require('../configValidation'); | ||
|
||
module.exports.tests = {}; | ||
|
||
module.exports.tests.interface = function(test, common) { | ||
test('config without schema should throw error', function(t) { | ||
var config = { | ||
esclient: {} | ||
}; | ||
|
||
t.throws(function() { | ||
configValidation.validate(config); | ||
}, /"schema" is required/, 'schema should exist'); | ||
t.end(); | ||
|
||
}); | ||
|
||
test('config without schema.indexName should throw error', function(t) { | ||
var config = { | ||
schema: {}, | ||
esclient: {} | ||
}; | ||
|
||
t.throws(function() { | ||
configValidation.validate(config); | ||
}, /"indexName" is required/, 'schema.indexName should exist'); | ||
t.end(); | ||
|
||
}); | ||
|
||
test('config with non-string schema.indexName should throw error', function(t) { | ||
[null, 17, {}, [], false].forEach((value) => { | ||
var config = { | ||
schema: { | ||
indexName: value | ||
}, | ||
esclient: {} | ||
}; | ||
|
||
t.throws(function() { | ||
configValidation.validate(config); | ||
}, /"indexName" must be a string/, 'schema.indexName should be a string'); | ||
|
||
}); | ||
|
||
t.end(); | ||
|
||
}); | ||
|
||
test('config with non-object esclient should throw error', function(t) { | ||
[null, 17, [], 'string', true].forEach((value) => { | ||
var config = { | ||
schema: { | ||
indexName: 'index name' | ||
}, | ||
esclient: value | ||
}; | ||
|
||
t.throws(function() { | ||
configValidation.validate(config); | ||
}, /"esclient" must be an object/, 'esclient should be an object'); | ||
|
||
}); | ||
|
||
t.end(); | ||
|
||
}); | ||
|
||
test('config with string schema.indexName and object esclient should not throw error', function(t) { | ||
var config = { | ||
schema: { | ||
indexName: 'index name' | ||
}, | ||
esclient: {} | ||
}; | ||
|
||
t.doesNotThrow(function() { | ||
configValidation.validate(config); | ||
}, 'no error should have been thrown'); | ||
|
||
t.end(); | ||
|
||
}); | ||
|
||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
|
||
function test(name, testFunction) { | ||
return tape('configValidation: ' + name, testFunction); | ||
} | ||
|
||
for( var testCase in module.exports.tests ){ | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
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