File size: 697 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict'

const { AVV_ERR_PLUGIN_NOT_VALID } = require('./errors')

/**
 * @param {any} maybePlugin
 * @throws {AVV_ERR_PLUGIN_NOT_VALID}
 *
 * @returns {asserts plugin is Function|PromiseLike}
 */
function validatePlugin (maybePlugin) {
  // validate if plugin is a function or Promise
  if (!(maybePlugin && (typeof maybePlugin === 'function' || typeof maybePlugin.then === 'function'))) {
    if (Array.isArray(maybePlugin)) {
      throw new AVV_ERR_PLUGIN_NOT_VALID('array')
    } else if (maybePlugin === null) {
      throw new AVV_ERR_PLUGIN_NOT_VALID('null')
    } else {
      throw new AVV_ERR_PLUGIN_NOT_VALID(typeof maybePlugin)
    }
  }
}

module.exports = {
  validatePlugin
}