All files / src/modules validateOptions.js

100% Statements 14/14
100% Branches 13/13
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                  39x   39x 1x     38x 70x 8x 1x   7x   62x 3x     59x   18x 4x   14x        
/**
 * Validate list of options for packagecloud API requests.
 * @module src/modules/validateOptions
 * @param {Object} options - Repository options.
 * @param {Array} requiredFields - An array of strings containing field names to validate for the API endpoint.
 * @return {Promise} The superagent promise object.
 */
export default (options, requiredFields) => {
 
  let opts = options || {};
 
  if(typeof opts !== "object") {
    throw new Error("The packagecloud client expects an object with a token field: {token: api_token}");
  }
  
  requiredFields.forEach(function(field) {
    if (!(field in opts)) {
      if(field === "token") {
        throw new Error("packagecloud API token is required: {token: packagecloud_api_token}");
      } else {
        throw new Error(`missing field: ${field}`);
      }
    } else if (!opts[field]) {
      throw new Error(`${field} cannot be null or undefined`);
    }
 
    switch(field) {
    case 'repo':
      if(opts.repo.split("/").length < 2) {
        throw new Error("The repo field must be in the format: username/reponame");
      }
      break;
    }
  });
}