all files / js/helper/ Validation.class.js

100% Statements 33/33
100% Branches 48/48
100% Functions 1/1
100% Lines 33/33
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                            370× 370×                                                                                               370×      
/**
 * @fileOverview Validation Class.
 * @author Simon Gattner <npm@0x38.de>
 * @license MIT
 * @version 1.0.2
 */
export default class Validation {
  /**
   * Do we have valid options?
   * @function Validation.options
   * @param {options} options to validate.
   * @return {object} options validated.
   */
  static options(options) {
    let error = '';
    switch (true) {
      case (typeof options !== 'object' || Array.isArray(options)):
        error = 'options must be an object';
        break;
      case (
        typeof options.message !== 'string' ||
        options.message.length === 0
      ):
        error = 'options.message must be a string > 0';
        break;
      case (
        typeof options.selector !== 'string' &&
        typeof options.selector !== 'undefined'
      ):
        error = 'options.selector must be a string or undefined';
        break;
      case (
        typeof options.selector === 'string' &&
        document.querySelector(options.selector) === null
      ):
        error = 'options.selector must be a DOM selector';
        break;
      case (
        typeof options.width !== 'number' &&
        typeof options.width !== 'undefined'
      ):
        error = 'options.width must be a number or undefined';
        break;
      case (typeof options.width === 'number' && options.width === 0):
        error = 'options.width must be a number > 0 or undefined';
        break;
      case (
        typeof options.height !== 'number' &&
        typeof options.height !== 'undefined'
      ):
        error = 'options.height must be a number or undefined';
        break;
      case (typeof options.height === 'number' && options.height === 0):
        error = 'options.height must be a number > 0 or undefined';
        break;
      case (
        typeof options.interval !== 'number' &&
        typeof options.interval !== 'undefined'
      ):
        error = 'options.interval must be a number or undefined';
        break;
      case (typeof options.interval === 'number' && options.interval === 0):
        error = 'options.interval must be a number > 0 or undefined';
        break;
      case (
        typeof options.possible !== 'string' &&
        typeof options.possible !== 'undefined'
      ):
        error = 'options.possible must be a string or undefined';
        break;
      case (
        typeof options.possible === 'string' &&
        options.possible.length === 0
      ):
        error = 'options.possible must be a string > 0 or undefined';
        break;
      case (
        !Array.isArray(options.style) &&
        typeof options.style !== 'undefined'
      ):
        error = 'options.style must be an array or undefined';
        break;
      case (Array.isArray(options.style) && options.style.length === 0):
        error = 'options.style must be an array > 0';
        break;
      case (
        typeof options.callback !== 'function' &&
        options.callback !== null &&
        typeof options.callback !== 'undefined'
      ):
        error = 'options.callback must be a function, null or undefined';
        break;
    }
    return (error.length === 0) ? options : {error: error};
  }
}