validate.js (3872B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // VARIABLES // 22 23 var hasOwnProp = Object.prototype.hasOwnProperty; 24 var isArray = Array.isArray; 25 26 27 // MAIN // 28 29 /** 30 * Validates function options. 31 * 32 * @private 33 * @param {Object} opts - destination object 34 * @param {Options} options - function options 35 * @param {Object} [options.pkg] - package meta information (package.json) 36 * @param {string} [options.version] - command-line interface version 37 * @param {string} [options.help] - help text 38 * @param {(string|boolean)} [options.title] - process title or a boolean indicating whether to set the process title 39 * @param {boolean} [options.updates] - boolean indicating whether to check if a command-line interface is an outdated version 40 * @param {Array} [options.argv] - command-line arguments 41 * @param {Options} [options.options] - command-line interface options 42 * @returns {(Error|null)} null or an error object 43 * 44 * @example 45 * var opts = {}; 46 * var options = { 47 * 'pkg': {}, 48 * 'version': '1.0.0', 49 * 'help': 'Usage: beep [options] <boop>', 50 * 'title': 'foo', 51 * 'updates': true 52 * }; 53 * var err = validate( opts, options ); 54 * if ( err ) { 55 * throw err; 56 * } 57 */ 58 function validate( opts, options ) { 59 if ( typeof options !== 'object' || options === null || isArray( options ) ) { 60 return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' ); 61 } 62 if ( hasOwnProp.call( options, 'pkg' ) ) { 63 opts.pkg = options.pkg; 64 if ( typeof opts.pkg !== 'object' || opts.pkg === null || isArray( opts.pkg ) ) { 65 return new TypeError( 'invalid option. `pkg` option must be an object. Option: `' + opts.pkg + '`.' ); 66 } 67 } 68 if ( hasOwnProp.call( options, 'help' ) ) { 69 opts.help = options.help; 70 if ( typeof opts.help !== 'string' ) { 71 return new TypeError( 'invalid option. `help` option must be a string primitive. Option: `' + opts.help + '`.' ); 72 } 73 } 74 if ( hasOwnProp.call( options, 'version' ) ) { 75 opts.version = options.version; 76 if ( typeof opts.version !== 'string' ) { 77 return new TypeError( 'invalid option. `version` option must be a string primitive. Option: `' + opts.version + '`.' ); 78 } 79 } 80 if ( hasOwnProp.call( options, 'title' ) ) { 81 opts.title = options.title; 82 if ( typeof opts.title !== 'string' && typeof opts.title !== 'boolean' ) { 83 return new TypeError( 'invalid option. `title` option must be either a string or boolean primitive. Option: `' + opts.title + '`.' ); 84 } 85 } 86 if ( hasOwnProp.call( options, 'updates' ) ) { 87 opts.updates = options.updates; 88 if ( typeof opts.updates !== 'boolean' ) { 89 return new TypeError( 'invalid option. `updates` option must be a boolean primitive. Option: `' + opts.updates + '`.' ); 90 } 91 } 92 if ( hasOwnProp.call( options, 'argv' ) ) { 93 opts.argv = options.argv; 94 if ( !isArray( opts.argv ) ) { 95 return new TypeError( 'invalid option. `argv` option must be an array. Option: `' + opts.argv + '`.' ); 96 } 97 } 98 if ( hasOwnProp.call( options, 'options' ) ) { 99 opts.options = options.options; 100 if ( typeof opts.options !== 'object' || opts.options === null || isArray( opts.options ) ) { 101 return new TypeError( 'invalid option. `options` option must be a plain object. Option: `' + opts.options + '`.' ); 102 } 103 } 104 return null; 105 } 106 107 108 // EXPORTS // 109 110 module.exports = validate;