Navigation Menu

Skip to content

Commit

Permalink
lib: avoid creating a throw away object in validateObject
Browse files Browse the repository at this point in the history
PR-URL: #39807
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
aduh95 authored and BethGriggs committed Sep 21, 2021
1 parent 9a67296 commit 6085280
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/internal/validators.js
Expand Up @@ -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' && (
Expand Down

0 comments on commit 6085280

Please sign in to comment.