Skip to content

Commit

Permalink
feat: Merge pull request #199 from pelias/add-config-validation
Browse files Browse the repository at this point in the history
added validation for configuration
  • Loading branch information
trescube authored Dec 29, 2016
2 parents fedbfae + 9c96eeb commit d057d3f
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 1 deletion.
24 changes: 24 additions & 0 deletions configValidation.js
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);
}
});
}

};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
"dependencies": {
"colors": "^1.1.2",
"elasticsearch": "^12.0.1",
"joi": "^10.1.0",
"mergeable": "latest",
"pelias-config": "2.4.0"
},
"devDependencies": {
"difflet": "^1.0.1",
"elastictest": "^1.2.0",
"proxyquire": "^1.7.10",
"tap-spec": "^4.1.1",
"tape": "^4.5.0",
"semantic-release": "^6.3.2"
Expand Down
4 changes: 4 additions & 0 deletions settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict';

var Mergeable = require('mergeable');
var peliasConfig = require('pelias-config');
var punctuation = require('./punctuation');
var street_suffix = require('./street_suffix');

require('./configValidation').validate(peliasConfig.generate());

var moduleDir = require('path').dirname("../");

function generate(){
Expand Down
100 changes: 100 additions & 0 deletions test/configValidation.js
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);
}
};
3 changes: 2 additions & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var tests = [
require('./partial-admin.js'),
require('./partial-literal.js'),
require('./partial-hash.js'),
require('./settings.js')
require('./settings.js'),
require('./configValidation.js')
];

tests.map(function(t) {
Expand Down
21 changes: 21 additions & 0 deletions test/settings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var path = require('path'),
settings = require('../settings'),
fs = require('fs');
Expand All @@ -11,6 +13,25 @@ module.exports.tests.interface = function(test, common) {
});
};

module.exports.tests.configValidation = function(test, common) {
test('configValidation throwing error should rethrow', function(t) {
t.throws(function() {
const proxyquire = require('proxyquire').noCallThru();
proxyquire('../settings', {
'./configValidation': {
validate: () => {
throw Error('config is not valid');
}
}
})();

}, /config is not valid/);

t.end();

});
}

module.exports.tests.compile = function(test, common) {
test('valid settings file', function(t) {
var s = settings();
Expand Down

0 comments on commit d057d3f

Please sign in to comment.