validate.js (3974B)
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 // MODULES // 22 23 var isObject = require( '@stdlib/assert/is-plain-object' ); 24 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 25 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; 26 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 27 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 28 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 29 30 31 // MAIN // 32 33 /** 34 * Validates function options. 35 * 36 * @private 37 * @param {Object} opts - destination object 38 * @param {Options} options - options to validate 39 * @param {string} [options.cmd] - executable file/command 40 * @param {PositiveInteger} [options.concurrency] - number of scripts to execute concurrently 41 * @param {PositiveInteger} [options.workers] - number of workers 42 * @param {boolean} [options.ordered] - boolean indicating whether to preserve the order of script output 43 * @param {NonNegativeInteger} [options.uid] - process user identity 44 * @param {NonNegativeInteger} [options.gid] - process group identity 45 * @param {NonNegativeInteger} [options.maxBuffer] - max child process `stdio` buffer size 46 * @returns {(Error|null)} error or null 47 * 48 * @example 49 * var opts = {}; 50 * var options = { 51 * 'concurrency': 4, 52 * 'workers': 2 53 * }; 54 * var err = validate( opts, options ); 55 * if ( err ) { 56 * throw err; 57 * } 58 */ 59 function validate( opts, options ) { 60 if ( !isObject( options ) ) { 61 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 62 } 63 if ( hasOwnProp( options, 'concurrency' ) ) { 64 opts.concurrency = options.concurrency; 65 if ( !isPositiveInteger( opts.concurrency ) ) { 66 return new TypeError( 'invalid option. `concurrency` option must be a positive integer. Option: `' + opts.concurrency + '`.' ); 67 } 68 } 69 if ( hasOwnProp( options, 'workers' ) ) { 70 opts.workers = options.workers; 71 if ( !isPositiveInteger( opts.workers ) ) { 72 return new TypeError( 'invalid option. `workers` option must be a positive integer. Option: `' + opts.workers + '`.' ); 73 } 74 } 75 if ( hasOwnProp( options, 'cmd' ) ) { 76 opts.cmd = options.cmd; 77 if ( !isString( opts.cmd ) ) { 78 return new TypeError( 'invalid option. `cmd` option must be a primitive string. Option: `' + opts.cmd + '`.' ); 79 } 80 } 81 if ( hasOwnProp( options, 'ordered' ) ) { 82 opts.ordered = options.ordered; 83 if ( !isBoolean( opts.ordered ) ) { 84 return new TypeError( 'invalid option. `ordered` option must be a primitive boolean. Option: `' + opts.ordered + '`.' ); 85 } 86 } 87 if ( hasOwnProp( options, 'uid' ) ) { 88 opts.uid = options.uid; 89 if ( !isNonNegativeInteger( opts.uid ) ) { 90 return new TypeError( 'invalid option. `uid` option must be a nonnegative integer. Option: `' + opts.uid + '`.' ); 91 } 92 } 93 if ( hasOwnProp( options, 'gid' ) ) { 94 opts.gid = options.gid; 95 if ( !isNonNegativeInteger( opts.gid ) ) { 96 return new TypeError( 'invalid option. `gid` option must be a nonnegative integer. Option: `' + opts.gid + '`.' ); 97 } 98 } 99 if ( hasOwnProp( options, 'maxBuffer' ) ) { 100 opts.maxBuffer = options.maxBuffer; 101 if ( !isNonNegativeInteger( opts.maxBuffer ) ) { 102 return new TypeError( 'invalid option. `maxBuffer` option must be a nonnegative integer. Option: `' + opts.maxBuffer + '`.' ); 103 } 104 } 105 return null; 106 } 107 108 109 // EXPORTS // 110 111 module.exports = validate;