@@ -177,7 +177,7 @@ function validateNumber(value, name, min = undefined, max) {
177
177
throw new ERR_INVALID_ARG_TYPE ( name , 'number' , value ) ;
178
178
179
179
if ( ( min != null && value < min ) || ( max != null && value > max ) ||
180
- ( ( min != null || max != null ) && NumberIsNaN ( value ) ) ) {
180
+ ( ( min != null || max != null ) && NumberIsNaN ( value ) ) ) {
181
181
throw new ERR_OUT_OF_RANGE (
182
182
name ,
183
183
`${ min != null ? `>= ${ min } ` : '' } ${ min != null && max != null ? ' && ' : '' } ${ max != null ? `<= ${ max } ` : '' } ` ,
@@ -218,41 +218,48 @@ function validateBoolean(value, name) {
218
218
throw new ERR_INVALID_ARG_TYPE ( name , 'boolean' , value ) ;
219
219
}
220
220
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 ;
232
225
233
226
/**
234
227
* @callback validateObject
235
228
* @param {* } value
236
229
* @param {string } name
237
- * @param {{
238
- * allowArray?: boolean,
239
- * allowFunction?: boolean,
240
- * nullable?: boolean
241
- * }} [options]
230
+ * @param {number } [options]
242
231
*/
243
232
244
233
/** @type {validateObject } */
245
234
const 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
+ }
256
263
}
257
264
} ) ;
258
265
@@ -564,6 +571,10 @@ module.exports = {
564
571
validateInteger,
565
572
validateNumber,
566
573
validateObject,
574
+ kValidateObjectNone,
575
+ kValidateObjectAllowNullable,
576
+ kValidateObjectAllowArray,
577
+ kValidateObjectAllowFunction,
567
578
validateOneOf,
568
579
validatePlainFunction,
569
580
validatePort,
0 commit comments