Skip to content

Commit

Permalink
Merge pull request #112 from pelias/greenkeeper/@hapi/joi-16.0.0
Browse files Browse the repository at this point in the history
Update @hapi/joi to the latest version 🚀
  • Loading branch information
orangejulius committed Nov 25, 2019
2 parents c9e51d4 + 99eff3c commit 113a7d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"tape": "^4.4.0"
},
"dependencies": {
"@hapi/joi": "^15.0.0",
"@hapi/joi": "^16.0.0",
"elasticsearch": "^16.0.0",
"pelias-config": "^4.5.0",
"pelias-logger": "^1.2.1",
Expand Down
58 changes: 26 additions & 32 deletions src/configValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,40 @@ const elasticsearch = require('elasticsearch');
// dbclient.statFrequency: populated by defaults if not overridden
// esclient: object, validation performed by elasticsearch module
const schema = Joi.object().keys({
dbclient: {
statFrequency: Joi.number().integer().min(0)
},
esclient: Joi.object().keys({
dbclient: Joi.object().required().keys({
statFrequency: Joi.number().integer().min(0).required()
}),
esclient: Joi.object().required().keys({
requestTimeout: Joi.number().integer().min(0)
}).unknown(true),
schema: Joi.object().keys({
indexName: Joi.string(),
typeName: Joi.string()
indexName: Joi.string().required(),
typeName: Joi.string().required()
})
}).requiredKeys(
'dbclient', 'dbclient.statFrequency', 'esclient',
'schema.indexName', 'schema.typeName'
).unknown(true);
}).unknown(true);

module.exports = {
validate: function validate(config) {
Joi.validate(config, schema, (err, value) => {
if (err) {
throw new Error(err.details[0].message);
const validate = schema.validate(config);
if (validate.error) {
throw new Error(validate.error.details[0].message);
}

// now verify that the index exists
const esclient = new elasticsearch.Client(config.esclient);

// callback that throws an error if the index doesn't exist
const existsCallback = (error, exists) => {
if (!exists) {
console.error(`ERROR: Elasticsearch index ${config.schema.indexName} does not exist`);
console.error('You must use the pelias-schema tool (https://github.com/pelias/schema/) to create the index first');
console.error('For full instructions on setting up Pelias, see http://pelias.io/install.html');

throw new Error(`elasticsearch index ${config.schema.indexName} does not exist`);
}
};

// now verify that the index exists
const esclient = new elasticsearch.Client(config.esclient);

// callback that throws an error if the index doesn't exist
const existsCallback = (error, exists) => {
if (!exists) {
console.error(`ERROR: Elasticsearch index ${config.schema.indexName} does not exist`);
console.error('You must use the pelias-schema tool (https://github.com/pelias/schema/) to create the index first');
console.error('For full instructions on setting up Pelias, see http://pelias.io/install.html');

throw new Error(`elasticsearch index ${config.schema.indexName} does not exist`);
}
};

// can also be done with promises but it's hard to test mixing the paradigms
esclient.indices.exists({ index: config.schema.indexName }, existsCallback);

});
// can also be done with promises but it's hard to test mixing the paradigms
esclient.indices.exists({ index: config.schema.indexName }, existsCallback);
}

};
20 changes: 10 additions & 10 deletions test/configValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"statFrequency" is required/, 'dbclient.statFrequency should exist');
}, /"dbclient.statFrequency" is required/, 'dbclient.statFrequency should exist');
t.end();

});
Expand All @@ -55,7 +55,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"statFrequency" must be a number/, 'dbclient.statFrequency should be a number');
}, /"dbclient.statFrequency" must be a number/, 'dbclient.statFrequency should be a number');

});

Expand All @@ -77,7 +77,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"statFrequency" must be an integer/, 'dbclient.statFrequency should be an integer');
}, /"dbclient.statFrequency" must be an integer/, 'dbclient.statFrequency should be an integer');

t.end();

Expand All @@ -98,7 +98,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"esclient" must be an object/, 'esclient should be an object');
}, /"esclient" must be of type object/, 'esclient should be an object');

});

Expand All @@ -123,7 +123,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"requestTimeout" must be a number/, 'esclient.requestTimeout should be a number');
}, /"esclient.requestTimeout" must be a number/, 'esclient.requestTimeout should be a number');
});

t.end();
Expand All @@ -146,7 +146,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"requestTimeout" must be an integer/, 'esclient.requestTimeout should be an integer');
}, /"esclient.requestTimeout" must be an integer/, 'esclient.requestTimeout should be an integer');

t.end();

Expand All @@ -168,7 +168,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"requestTimeout" must be larger than or equal to 0/, 'esclient.requestTimeout must be positive');
}, /"esclient.requestTimeout" must be larger than or equal to 0/, 'esclient.requestTimeout must be positive');

t.end();

Expand All @@ -186,7 +186,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"schema" must be an object/);
}, /"schema" must be of type object/);

});

Expand All @@ -208,7 +208,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"indexName" must be a string/);
}, /"schema.indexName" must be a string/);

});

Expand All @@ -227,7 +227,7 @@ module.exports.tests.validate = function(test, common) {

t.throws(function() {
configValidation.validate(config);
}, /"indexName" is required/);
}, /"schema.indexName" is required/);
t.end();

});
Expand Down

0 comments on commit 113a7d8

Please sign in to comment.