diff --git a/configValidation.js b/configValidation.js index 6131d123..f2f65452 100644 --- a/configValidation.js +++ b/configValidation.js @@ -3,22 +3,19 @@ const Joi = require('@hapi/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(), - typeName: Joi.string() - }, - esclient: Joi.object() -}).requiredKeys( - 'schema', 'schema.indexName', 'schema.typeName', 'esclient' -).unknown(true); +const schema = Joi.object().required().keys({ + schema: Joi.object().required().keys({ + indexName: Joi.string().required(), + typeName: Joi.string().required() + }), + esclient: Joi.object().required() +}).unknown(true); module.exports = { validate: function validate(config) { - Joi.validate(config, schema, err => { - if (err) { - throw new Error(err.details[0].message); - } - }); + const validated = schema.validate(config); + if (validated.error) { + throw new Error(validated.error.details[0].message); + } } }; diff --git a/test/configValidation.js b/test/configValidation.js index b44dc469..d4a2a676 100644 --- a/test/configValidation.js +++ b/test/configValidation.js @@ -25,7 +25,7 @@ module.exports.tests.interface = function(test, common) { t.throws(function() { configValidation.validate(config); - }, /"indexName" is required/, 'schema.indexName should exist'); + }, /"schema.indexName" is required/, 'schema.indexName should exist'); t.end(); }); @@ -38,7 +38,7 @@ module.exports.tests.interface = function(test, common) { t.throws(function () { configValidation.validate(config); - }, /"typeName" is required/, 'schema.typeName should exist'); + }, /"schema.typeName" is required/, 'schema.typeName should exist'); t.end(); }); @@ -55,7 +55,7 @@ module.exports.tests.interface = function(test, common) { t.throws(function() { configValidation.validate(config); - }, /"indexName" must be a string/, 'schema.indexName should be a string'); + }, /"schema.indexName" must be a string/, 'schema.indexName should be a string'); }); @@ -75,7 +75,7 @@ module.exports.tests.interface = function(test, common) { t.throws(function () { configValidation.validate(config); - }, /"typeName" must be a string/, 'schema.typeName should be a string'); + }, /"schema.typeName" must be a string/, 'schema.typeName should be a string'); }); @@ -95,7 +95,7 @@ module.exports.tests.interface = 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'); });