diff --git a/index.js b/index.js index f5f8d36..423ebf3 100644 --- a/index.js +++ b/index.js @@ -31,19 +31,19 @@ const SCHEMA_ADDRESSES = Joi.array() .min(1); const SCHEMA_STATUSES = Joi.array() .items(schema.plugins.notifications.schemaStatus) - .min(1); + .min(0); const SCHEMA_EMAIL = Joi.alternatives().try( Joi.object().keys({ addresses: SCHEMA_ADDRESSES, statuses: SCHEMA_STATUSES }), SCHEMA_ADDRESS, SCHEMA_ADDRESSES ); -const SCHEMA_BUILD_SETTINGS = Joi.object() +const SCHEMA_EMAIL_SETTINGS = Joi.object() .keys({ email: SCHEMA_EMAIL.required() }).unknown(true); const SCHEMA_BUILD_DATA = Joi.object() .keys({ ...schema.plugins.notifications.schemaBuildData, - settings: SCHEMA_BUILD_SETTINGS.required() + settings: SCHEMA_EMAIL_SETTINGS.required() }); const SCHEMA_SMTP_CONFIG = Joi.object() .keys({ @@ -181,7 +181,7 @@ class EmailNotifier extends NotificationBase { // Validate the settings email object static validateConfig(config) { - return Joi.validate(config, SCHEMA_EMAIL); + return SCHEMA_EMAIL_SETTINGS.validate(config); } } diff --git a/test/index.test.js b/test/index.test.js index ec07c2a..b44c2c5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -512,6 +512,98 @@ describe('index', () => { assert.equal(err.name, 'ValidationError'); } }); + + it('is valid config with complete parameters', () => { + configMock = { + email: { + addresses: ['notify.me@email.com', 'notify.you@email.com'], + statuses: ['SUCCESS', 'FAILURE'] + } + }; + + const res = EmailNotifier.validateConfig(configMock); + + assert.isUndefined(res.error); + }); + + it('is valid config with empty statuses', () => { + configMock = { + email: { + addresses: ['notify.me@email.com', 'notify.you@email.com'], + statuses: [] + } + }; + + const { error } = EmailNotifier.validateConfig(configMock); + + assert.isUndefined(error); + }); + + it('is valid config with addresses', () => { + configMock = { + email: { + addresses: ['notify.me@email.com', 'notify.you@email.com'] + } + }; + + const { error } = EmailNotifier.validateConfig(configMock); + + assert.isUndefined(error); + }); + + it('is invalid config with empty parameters', () => { + configMock = {}; + const { error } = EmailNotifier.validateConfig(configMock); + + assert.instanceOf(error, Error); + assert.equal(error.name, 'ValidationError'); + }); + + it('valid config with empty email settings', () => { + configMock = { + email: {} + }; + const { error } = EmailNotifier.validateConfig(configMock); + + assert.isUndefined(error); + }); + + it('valid config without addresses', () => { + configMock = { + email: { + statuses: ['SUCCESS', 'FAILURE'] + } + }; + const { error } = EmailNotifier.validateConfig(configMock); + + assert.isUndefined(error); + }); + + it('invalid config with empty addresses', () => { + configMock = { + email: { + addresses: [], + statuses: ['SUCCESS', 'FAILURE'] + } + }; + const { error } = EmailNotifier.validateConfig(configMock); + + assert.instanceOf(error, Error); + assert.equal(error.name, 'ValidationError'); + }); + + it('invalid unknown status', () => { + configMock = { + email: { + addresses: ['notify.me@email.com'], + statuses: ['DUMMY_STATUS'] + } + }; + const { error } = EmailNotifier.validateConfig(configMock); + + assert.instanceOf(error, Error); + assert.equal(error.name, 'ValidationError'); + }); }); describe('buildData is validated', () => {