@@ -177,7 +177,7 @@ function validateNumber(value, name, min = undefined, max) {
177177 throw new ERR_INVALID_ARG_TYPE ( name , 'number' , value ) ;
178178
179179 if ( ( min != null && value < min ) || ( max != null && value > max ) ||
180- ( ( min != null || max != null ) && NumberIsNaN ( value ) ) ) {
180+ ( ( min != null || max != null ) && NumberIsNaN ( value ) ) ) {
181181 throw new ERR_OUT_OF_RANGE (
182182 name ,
183183 `${ min != null ? `>= ${ min } ` : '' } ${ min != null && max != null ? ' && ' : '' } ${ max != null ? `<= ${ max } ` : '' } ` ,
@@ -218,41 +218,48 @@ function validateBoolean(value, name) {
218218 throw new ERR_INVALID_ARG_TYPE ( name , 'boolean' , value ) ;
219219}
220220
221- /**
222- * @param {any } options
223- * @param {string } key
224- * @param {boolean } defaultValue
225- * @returns {boolean }
226- */
227- function getOwnPropertyValueOrDefault ( options , key , defaultValue ) {
228- return options == null || ! ObjectPrototypeHasOwnProperty ( options , key ) ?
229- defaultValue :
230- options [ key ] ;
231- }
221+ const kValidateObjectNone = 0 ;
222+ const kValidateObjectAllowNullable = 1 << 0 ;
223+ const kValidateObjectAllowArray = 1 << 1 ;
224+ const kValidateObjectAllowFunction = 1 << 2 ;
232225
233226/**
234227 * @callback validateObject
235228 * @param {* } value
236229 * @param {string } name
237- * @param {{
238- * allowArray?: boolean,
239- * allowFunction?: boolean,
240- * nullable?: boolean
241- * }} [options]
230+ * @param {number } [options]
242231 */
243232
244233/** @type {validateObject } */
245234const validateObject = hideStackFrames (
246- ( value , name , options = null ) => {
247- const allowArray = getOwnPropertyValueOrDefault ( options , 'allowArray' , false ) ;
248- const allowFunction = getOwnPropertyValueOrDefault ( options , 'allowFunction' , false ) ;
249- const nullable = getOwnPropertyValueOrDefault ( options , 'nullable' , false ) ;
250- if ( ( ! nullable && value === null ) ||
251- ( ! allowArray && ArrayIsArray ( value ) ) ||
252- ( typeof value !== 'object' && (
253- ! allowFunction || typeof value !== 'function'
254- ) ) ) {
255- throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
235+ ( value , name , options = kValidateObjectNone ) => {
236+ if ( options === kValidateObjectNone ) {
237+ if ( value === null || ArrayIsArray ( value ) ) {
238+ throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
239+ }
240+
241+ if ( typeof value !== 'object' ) {
242+ throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
243+ }
244+ } else {
245+ const throwOnNullable = ( kValidateObjectAllowNullable & options ) === 0 ;
246+
247+ if ( throwOnNullable && value === null ) {
248+ throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
249+ }
250+
251+ const throwOnArray = ( kValidateObjectAllowArray & options ) === 0 ;
252+
253+ if ( throwOnArray && ArrayIsArray ( value ) ) {
254+ throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
255+ }
256+
257+ const throwOnFunction = ( kValidateObjectAllowFunction & options ) === 0 ;
258+ const typeofValue = typeof value ;
259+
260+ if ( typeofValue !== 'object' && ( throwOnFunction || typeofValue !== 'function' ) ) {
261+ throw new ERR_INVALID_ARG_TYPE ( name , 'Object' , value ) ;
262+ }
256263 }
257264 } ) ;
258265
@@ -564,6 +571,10 @@ module.exports = {
564571 validateInteger,
565572 validateNumber,
566573 validateObject,
574+ kValidateObjectNone,
575+ kValidateObjectAllowNullable,
576+ kValidateObjectAllowArray,
577+ kValidateObjectAllowFunction,
567578 validateOneOf,
568579 validatePlainFunction,
569580 validatePort,
0 commit comments