-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract validatePlugin and create isBundledOrTypescriptPlugin (#216)
* extract validatePlugin and create isBundledOrTypescriptPlugin more fine grained error message * fix test label * add jsdoc * adapt metcoders suggestion
- Loading branch information
Showing
5 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
/** | ||
* bundled or typescript plugin | ||
* @typedef {object} BundledOrTypescriptPlugin | ||
* @property {function} default | ||
*/ | ||
|
||
/** | ||
* @param {unknown} plugin | ||
* @returns {plugin is BundledOrTypescriptPlugin} | ||
*/ | ||
|
||
function isBundledOrTypescriptPlugin (plugin) { | ||
return plugin !== null && typeof plugin === 'object' && typeof plugin.default === 'function' | ||
} | ||
|
||
module.exports = { | ||
isBundledOrTypescriptPlugin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const { AVV_ERR_PLUGIN_NOT_VALID } = require('./errors') | ||
|
||
/** | ||
* @param {unknown} plugin | ||
* @throws {AVV_ERR_PLUGIN_NOT_VALID} | ||
* @returns {Function} | ||
*/ | ||
function validatePlugin (plugin) { | ||
// validate if plugin is a function or Promise | ||
if (!(plugin && (typeof plugin === 'function' || typeof plugin.then === 'function'))) { | ||
if (Array.isArray(plugin)) { | ||
throw new AVV_ERR_PLUGIN_NOT_VALID('array') | ||
} else if (plugin === null) { | ||
throw new AVV_ERR_PLUGIN_NOT_VALID('null') | ||
} else { | ||
throw new AVV_ERR_PLUGIN_NOT_VALID(typeof plugin) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
validatePlugin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { isBundledOrTypescriptPlugin } = require('../../lib/is-bundled-or-typescript-plugin') | ||
|
||
test('isBundledOrTypescriptPlugin', (t) => { | ||
t.plan(9) | ||
|
||
t.equal(isBundledOrTypescriptPlugin(1), false) | ||
t.equal(isBundledOrTypescriptPlugin('function'), false) | ||
t.equal(isBundledOrTypescriptPlugin({}), false) | ||
t.equal(isBundledOrTypescriptPlugin([]), false) | ||
t.equal(isBundledOrTypescriptPlugin(null), false) | ||
|
||
t.equal(isBundledOrTypescriptPlugin(function () {}), false) | ||
t.equal(isBundledOrTypescriptPlugin(new Promise((resolve) => resolve)), false) | ||
t.equal(isBundledOrTypescriptPlugin(Promise.resolve()), false) | ||
|
||
t.equal(isBundledOrTypescriptPlugin({ default: () => {} }), true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { validatePlugin } = require('../../lib/validate-plugin') | ||
const { AVV_ERR_PLUGIN_NOT_VALID } = require('../../lib/errors') | ||
|
||
test('validatePlugin', (t) => { | ||
t.plan(8) | ||
|
||
t.throws(() => validatePlugin(1), new AVV_ERR_PLUGIN_NOT_VALID('number')) | ||
t.throws(() => validatePlugin('function'), new AVV_ERR_PLUGIN_NOT_VALID('string')) | ||
t.throws(() => validatePlugin({}), new AVV_ERR_PLUGIN_NOT_VALID('object')) | ||
t.throws(() => validatePlugin([]), new AVV_ERR_PLUGIN_NOT_VALID('array')) | ||
t.throws(() => validatePlugin(null), new AVV_ERR_PLUGIN_NOT_VALID('null')) | ||
|
||
t.doesNotThrow(() => validatePlugin(function () {})) | ||
t.doesNotThrow(() => validatePlugin(new Promise((resolve) => resolve))) | ||
t.doesNotThrow(() => validatePlugin(Promise.resolve())) | ||
}) |