From 608528028ca3a7ba77a76cf57b1710f81ff8e87e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 19 Aug 2021 12:20:04 +0200 Subject: [PATCH] lib: avoid creating a throw away object in `validateObject` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/39807 Reviewed-By: Luigi Pinca Reviewed-By: Michaƫl Zasso --- lib/internal/validators.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index b3a7ee26554e1a..81329160f07323 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -140,12 +140,21 @@ function validateBoolean(value, name) { throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value); } +/** + * @param {unknown} value + * @param {string} name + * @param {{ + * allowArray?: boolean, + * allowFunction?: boolean, + * nullable?: boolean + * }} [options] + */ const validateObject = hideStackFrames( - (value, name, { - nullable = false, - allowArray = false, - allowFunction = false, - } = {}) => { + (value, name, options) => { + const useDefaultOptions = options == null; + const allowArray = useDefaultOptions ? false : options.allowArray; + const allowFunction = useDefaultOptions ? false : options.allowFunction; + const nullable = useDefaultOptions ? false : options.nullable; if ((!nullable && value === null) || (!allowArray && ArrayIsArray(value)) || (typeof value !== 'object' && (