diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8117b96a2d40a4..67ddc0dc129f8e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -83,8 +83,9 @@ class Event { constructor(type, options = null) { if (arguments.length === 0) throw new ERR_MISSING_ARGS('type'); - if (options !== null) - validateObject(options, 'options'); + validateObject(options, 'options', { + allowArray: true, allowFunction: true, nullable: true, + }); const { cancelable, bubbles, composed } = { ...options }; this[kCancelable] = !!cancelable; this[kBubbles] = !!bubbles; @@ -542,7 +543,9 @@ function shouldAddListener(listener) { function validateEventListenerOptions(options) { if (typeof options === 'boolean') return { capture: options }; - validateObject(options, 'options'); + validateObject(options, 'options', { + allowArray: true, allowFunction: true, + }); return { once: Boolean(options.once), capture: Boolean(options.capture), diff --git a/lib/internal/validators.js b/lib/internal/validators.js index ac54c96bb2dec0..ebd2b5222dcfe4 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -150,10 +150,16 @@ function validateBoolean(value, name) { } const validateObject = hideStackFrames( - (value, name, { nullable = false } = {}) => { + (value, name, { + nullable = false, + allowArray = false, + allowFunction = false, + } = {}) => { if ((!nullable && value === null) || - ArrayIsArray(value) || - typeof value !== 'object') { + (!allowArray && ArrayIsArray(value)) || + (typeof value !== 'object' && ( + !allowFunction || typeof value !== 'function' + ))) { throw new ERR_INVALID_ARG_TYPE(name, 'Object', value); } }); diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 1918394fbae5e1..07523f02bdf10f 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -61,7 +61,6 @@ let asyncTest = Promise.resolve(); 'foo', 1, false, - function() {}, ].forEach((i) => ( throws(() => new Event('foo', i), { code: 'ERR_INVALID_ARG_TYPE', diff --git a/test/parallel/test-validators.js b/test/parallel/test-validators.js index cf4ba17f52e9d7..55c73b2d86d388 100644 --- a/test/parallel/test-validators.js +++ b/test/parallel/test-validators.js @@ -78,14 +78,17 @@ const invalidArgValueError = { validateObject({}, 'foo'); validateObject({ a: 42, b: 'foo' }, 'foo'); - [undefined, null, true, false, 0, 0.0, 42, '', 'string', []] + [undefined, null, true, false, 0, 0.0, 42, '', 'string', [], () => {}] .forEach((val) => { assert.throws(() => { validateObject(val, 'foo'); }, invalidArgTypeError); }); + // validateObject options tests: validateObject(null, 'foo', { nullable: true }); + validateObject([], 'foo', { allowArray: true }); + validateObject(() => {}, 'foo', { allowFunction: true }); } {