Skip to content

Commit

Permalink
Events validation. Closes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 28, 2017
1 parent 5044318 commit 73d3cac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
26 changes: 22 additions & 4 deletions lib/index.js
Expand Up @@ -38,7 +38,7 @@ internals.schema.listener = internals.schema.event.keys({
});


exports = module.exports = internals.Podium = function (events) {
exports = module.exports = internals.Podium = function (events, options) {

// Use descriptive names to avoid conflict when inherited

Expand All @@ -50,7 +50,7 @@ exports = module.exports = internals.Podium = function (events) {
this.onPodiumError = null;

if (events) {
this.registerEvent(events);
this.registerEvent(events, options);
}
};

Expand All @@ -69,7 +69,23 @@ internals.Podium.decorate = function (target, source) {
};


internals.Podium.prototype.registerEvent = function (events) {
internals.Podium.validate = function (events) {

const normalized = [];
[].concat(events).forEach((event) => {

This comment has been minimized.

Copy link
@AdriVanHoudt

AdriVanHoudt Sep 28, 2017

Contributor

Why not use .map?

This comment has been minimized.

Copy link
@hueniverse

hueniverse Sep 28, 2017

Author Contributor

Habit. Not reason.


if (typeof event === 'string') {
event = { name: event };
}

normalized.push(Joi.attempt(event, internals.schema.event, 'Invalid event options'));
});

return normalized;
};


internals.Podium.prototype.registerEvent = function (events, options = {}) {

events = Hoek.flatten([].concat(events));
events.forEach((event) => {
Expand All @@ -86,7 +102,9 @@ internals.Podium.prototype.registerEvent = function (events) {
event = { name: event };
}

event = Joi.attempt(event, internals.schema.event, 'Invalid event options');
if (options.validate !== false) { // Defaults to true
event = Joi.attempt(event, internals.schema.event, 'Invalid event options');
}

const name = event.name;
if (this._eventListeners[name]) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "podium",
"description": "Node compatible event emitter with extra features",
"version": "2.0.3",
"version": "2.1.0",
"repository": "git://github.com/hapijs/podium",
"main": "lib/index.js",
"keywords": [
Expand Down
34 changes: 34 additions & 0 deletions test/index.js
Expand Up @@ -700,6 +700,22 @@ describe('Podium', () => {
});
});

describe('validate()', () => {

it('normalizes events', async () => {

expect(Podium.validate('a')).to.equal([{ name: 'a' }]);
});

it('errors on invalid event property', async () => {

expect(() => {

Podium.validate({ name: 'a', unknown: 'x' });
}).to.throw(/Invalid event options/);
});
});

describe('registerEvent()', () => {

it('combines multiple sources', (done) => {
Expand Down Expand Up @@ -742,6 +758,24 @@ describe('Podium', () => {
source.registerEvent('a');
}).to.throw('Event a exists');
});

it('errors on invalid event property', async () => {

const source = new Podium();
expect(() => {

source.registerEvent({ name: 'a', unknown: 'x' });
}).to.throw(/Invalid event options/);
});

it('ignores invalid event property', async () => {

const source = new Podium();
expect(() => {

source.registerEvent({ name: 'a', unknown: 'x' }, { validate: false });
}).to.not.throw();
});
});

describe('registerPodium()', () => {
Expand Down

0 comments on commit 73d3cac

Please sign in to comment.